2020.7.31更新
class Solution {
public:
char firstUniqChar(string s) {
char res =' ';
if (s.size() == 0) return res;//为空
map<char, int> m;
for (auto c : s) {
m[c]++;
}
for (auto c : s) {
if (m[c] == 1) {
return c;
}
}
return res;
}
};
解题思路
先统计出每个字符的出现次数
再按照s中字符的顺序进行遍历
代码
class Solution {
public:
char firstUniqChar(string s) {
char res=' ';
map<char, int> hashMap;
for (auto it=s.begin();it!=s.end();it++)
{
++hashMap[*it];
}
for (auto it = s.begin(); it != s.end(); it++)
{
if (hashMap[*it] == 1) return *it;
}
return res;
}
};