新解法待续
6ms
public static int firstUniqChar(String s) {
int[] arr = new int[27];
for (int i = 0; i < s.length(); i++) {
arr[s.charAt(i) - 97]++;
}
for (int j = 0; j < s.length(); j++) {
if(arr[s.charAt(j) - 97] == 1) {
return j;
}
}
return -1;
}
55ms
public static int firstUniqChar(String s) {
Set<Integer> set = new LinkedHashSet<>();
for (int i = 0; i < s.length(); i++) {
Integer l = s.charAt(i) + 0;
if(set.contains(l) || set.contains(l + 127)) {
set.remove(l);
set.add(l + 127);
}else {
set.add(l);
}
}
set.add(-1);
return s.indexOf(set.stream().filter(v -> v < 127).findFirst().get());
}