**题目:**给定一个字符串,找到它的第一个不重复的字符,并返回它的索引,。如果不存在,则返回-1。 示例:
class Solution {
public int firstUniqChar(String s
) {
if(s
==null
||s
.length()==0)return -1;
Map
<Character,Integer> map
=new HashMap<Character,Integer>();
int i
=0;
while(i
<s
.length()){
map
.put(s
.charAt(i
),map
.getOrDefault(s
.charAt(i
),0)+1);
i
++;
}
i
=0;
for(;i
<s
.length();i
++) {
if(map
.get(s
.charAt(i
))==1) {
return i
;
}
}
return -1;
}
}
转载请注明原文地址: https://lol.8miu.com/read-33055.html