力扣题解 387th 字符串中的第一个唯一字符

387th 字符串中的第一个唯一字符

  • 哈希思想

    不重复的字符经过哈希表统计/映射后,其值为1。需要注意的是采用hash table映射后不能按a-z这样的英文字母表顺序查询第一个不重复字符,需要按照字符串原有的顺序线性查找。

    class Solution {
        public int firstUniqChar(String s) {
            int[] table = new int[(char)'z'+1];
            for(int i = 0; i < s.length(); i++) {
                table[s.charAt(i)]++;
            }
            
            for(int i = 0; i < s.length(); i++) {
                if(table[s.charAt(i)]==1) {
                    return i;
                }
            }
            return -1;
        }
    }
    
原文地址:https://www.cnblogs.com/fromneptune/p/13246116.html