题目:
Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.
思路:
扫描两遍数组即可。
代码实现:
class Solution {
public:
int firstUniqChar(string s) {
vector<int> hash(26, 0);
for (int i = 0; i < s.size(); ++i){
++hash[s[i]-'a'];
}
for (int i = 0; i < s.size(); ++i){
if (hash[s[i]-'a'] == 1){
return i;
}
}
return -1;
}
};