problem
solution
1 | class Solution { |
- vector
1
2
3
4
5
6
7
8
9
10
11
12class Solution {
public:
int firstUniqChar(string s) {
vector<int> vec(26,0);
for(char c:s) vec[c-'a']++;
int n = s.size();
for(int i=0;i<n;++i){
if(vec[s[i] - 'a'] ==1) return i;
}
return -1;
}
};
analysis
- time complexity
O(n)
- space complexity
O(1)
最多26字母