题目描述:
在字符串中找出第一个只出现一次的字符。
如输入
"abaccdeff"
,则输出b
。如果字符串中不存在只出现一次的字符,返回#字符。
样例:
输入:"abaccdeff"
输出:'b'
算法:
哈希法
class Solution {
public:
char firstNotRepeatingChar(string s) {
if(s.length()<=0)
return '#';
map<char, int>m;
for(int i=0; i<s.length(); i++)
{
m[s[i]] ++;
}
for(int i=0; i<s.length(); i++)
{
if(m[s[i]]==1)
return s[i];
}
return '#';
}
};