My code:
public class Solution {
public int firstUniqChar(String s) {
if (s == null || s.length() == 0) {
return -1;
}
int[] dict = new int[26];
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
dict[curr - 'a']++;
}
for (int i = 0; i < s.length(); i++) {
if (dict[s.charAt(i) - 'a'] == 1) {
return i;
}
}
return -1;
}
}
reference:
https://discuss.leetcode.com/topic/55148/java-7-lines-solution-29ms/4
本来以为会有什么更高端的解法, 所以看了答案,发现差不多。
只不过我又忘记了用数组更好。
Anyway, Good luck, Richardo! — 10/11/2016