题目
https://leetcode-cn.com/problems/first-unique-character-in-a-string/
哈希
使用数组统计每个字符出现的次数,从左向右遍历字符串寻找第一个只出现一次的字符,并返回其下标。
class Solution {
public int firstUniqChar(String s) {
int[] map = new int[26];
for (char c : s.toCharArray()) {
map[c - 'a']++;
}
for (int i = 0; i < s.length(); i++) {
// System.out.println(map[s.charAt(i) - 'a'] + " " + i);
if (map[s.charAt(i) - 'a'] == 1) {
return i;
}
}
return -1;
}
}