剑指 Offer 50. 第一个只出现一次的字符
题目链接:题目链接
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例 1:
输入:s = "abaccdeff"
输出:'b'
示例 2:
输入:s = ""
输出:' '
限制:
0 <= s 的长度 <= 50000
使用哈希表存储频数:
class Solution {
public:
char firstUniqChar(string s) {
unordered_map<int, int> count;
for(char ch:s){
count[ch]++;
}
for(int i = 0; i < s.size(); i++){
if(count[s[i]] == 1){
return s[i];
}
}
return ' ';
}
};