(C++)剑指offer-拓展:字符串中第一个只出现一次的字符
通过哈希表记录每个字符的出现的次数,遍历字符串一次,当第一个哈希表的值为1时,对应的字符即为所求,直接break结束,否则res等于#,整体时间复杂度为O(n)。过程较为简单,代码具体如下:
class Solution {
public:
char firstNotRepeatingChar(string s) {
unordered_map<char, int> count;
for(auto c : s) count[c]++;
char res = '#'; //opps
for(auto c : s){
if(count[c] == 1){
res = c;
break;
}
}
return res;
}
};