解题思路:先用一个map存放string里面每个字符出现的次数,然后遍历string每个字符,如果字符出现的次数为1就返回index,否则就返回-1
class Solution {
public:
int firstUniqChar(string s) {
map<char,int> s_m;
for(int i = 0; i < s.length(); i++){
s_m[s[i]]++;
}
for(int i = 0; i < s.length(); i++)
if (s_m.find(s[i])->second == 1)
return i;
return -1;
}
};