Skip to content

Commit 7428577

Browse files
authored
Merge pull request gzc426#176 from 1006685929/master
666
2 parents c3dfd2d + 89b3ddf commit 7428577

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public static int firstUniqChar(String s) {
2+
if(null == s || 0 == s.length()){
3+
return -1;
4+
}
5+
int[] hash = new int[26];
6+
char[] array = s.toCharArray();//s转为字符数组
7+
for(int i = 0; i < array.length; i++){
8+
int num = array[i] - 'a'; //表示这个字母在26个字母中所在的位置
9+
hash[num]++;
10+
}
11+
for(int i = 0; i < array.length; i++){//从字符串出发
12+
int num = array[i] - 'a';
13+
if(hash[num] == 1){
14+
return i;
15+
}
16+
}
17+
return -1;
18+
}

0 commit comments

Comments
 (0)