50. 第一个只出现一次的字符
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
map
class Solution {
public:
char firstUniqChar(string s) {
map<char, int>count;
for(char c:s){
count[c]++;
}
for(char c:s){
if(count[c] == 1)
return c;
}
return ' ';
}
};
数字代替哈希
class Solution {
public:
char firstUniqChar(string s) {
int count[26] = {0};
for(char c:s){
count[c-'a']++;
}
for(char c:s){
if(count[c-'a'] == 1)
return c;
}
return ' ';
}
};