题解 | #字符流中第一个不重复的字符#
字符流中第一个不重复的字符
https://www.nowcoder.com/practice/00de97733b8e4f97a3fb5c680ee10720
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param ch char字符型 * @return 无 */ static char st[1001] = {0}; //存储字符流的栈 static int curSt = -1; //栈顶指针 static char hash[26] = {0}; //存储字符出现的次数 void Insert(char ch ) { // write code here st[++curSt] = ch; } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param 无 * @return char字符型 */ char FirstAppearingOnce() { // write code here //将栈顶元素的出现次数同步到hash hash[st[curSt]-'a']++; //遍历栈的元素,与hash对比,返回只出现一次的字符 for(int i=0; i<=curSt; i++){ if(hash[st[i]-'a'] == 1) return st[i]; } return '#'; //遍历找不到只出现一次的字符,返回# }