Leetcode - First Unique Character in a String

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

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/62968597c53c
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞