文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Two loops
1 | class Solution { |
- Nested loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23class Solution {
public:
int firstUniqChar(string s) {
int index[256] = {0};
for(int i = 0; i < s.size(); i++) {
index[s[i]]++;
if(index[s[i]] != 1) {
continue;
}
bool flag = false;
for(int j = i + 1; j < s.size(); j++) {
if(s[i] == s[j]) {
flag = true;
break;
}
}
if(!flag) {
return i;
}
}
return -1;
}
};