統(tǒng)計字符串中每個字符出現(xiàn)的次數(shù)撒桨,然后再從左到右遍歷字符串,找出出現(xiàn)次數(shù)為1的字符串键兜。
class Solution {
public:
int FirstNotRepeatingChar(string str) {
if(!str.size()) return -1;
map<char,int> counter;
for(int i = 0;i<str.size();i++){
counter[str[i]]++;
}
for(int i = 0;i<str.size();i++){
if(counter[str[i]]==1)
return i;
}
return -1;
}
};