387. First Unique Character in a String

原始代码:

    //用一个int[26]记录小写字母出现次数,最后遍历一次,返回第一个值是1的index
    public int firstUniqChar(String s) {
        int[] map = new int[26];
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i) - 'a']++;
        }

        for (int i = 0; i < s.length(); i++) {
            if (map[s.charAt(i) - 'a'] == 1) return i;
        }
        return -1;
    }

你可能感兴趣的:(387. First Unique Character in a String)