给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
示例:
s = "leetcode"
返回 0
s = "loveleetcode"
返回 2
class Solution {
public int firstUniqChar(String s) {
HashMap<Character, Integer> map = new HashMap<>();
// 统计各字符出现次数;
for (char ch : s.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0)+1);
}
// 遍历s,将s中字符作为key,当value为1时,表示该字符唯一,返回i。
for (int i = 0; i < s.length(); i++) {
if (map.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}
}