搜索
您的当前位置:首页正文

Leetcode - First Unique Characte

来源:二三娱乐

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;
    }
}

本来以为会有什么更高端的解法, 所以看了答案,发现差不多。
只不过我又忘记了用数组更好。

Anyway, Good luck, Richardo! -- 10/11/2016

Top