LeetCode 3. Longest Substring Without Repeating Characters

击败91%,不知道怎么改进了。
代码如下:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] counts = new int[128];//标记字符出现位置
         for(int i = 0;i<counts.length;i++){
            counts[i]=-1;//初始化
        }
        char []src = s.toCharArray();
        int index = 0;
        int max = 0;
        for(int i = 0 ; i<src.length;i++){
            if(counts[ src[i] ]<index){
                counts[src[i]] = i;
                max = Math.max(max,i-index+1);
            }else{
                index = counts[src[i]]+1;
                counts[src[i]] = i;
            }
        }
        return max;
    }

}

你可能感兴趣的:(java,LeetCode,substring)