Java use another array to record the index 18m


  • 0
    F
        public int firstUniqChar(String s) {
            if(s==null || s.length()==0) return -1;
            int[] index = new int[256];
            char[] arr = s.toCharArray();
            for(int i=0;i<arr.length;i++){
                if(index[arr[i]]!=0) index[arr[i]] = -1;
                else index[arr[i]] = i+1;
            }
            int res = Integer.MAX_VALUE;
            for(int i=0;i<index.length;i++){
                if(index[i]>0){
                    res = Math.min(res, index[i]);
                }
            }
            return res==Integer.MAX_VALUE?-1:res-1;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.