Java无重复字符的最长子串 -----(笔记)

Java无重复字符的最长子串 -----(笔记)

public class Test {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(lengthOfLongestSubstring("abbcd"));
    }
    public static int lengthOfLongestSubstring(String s) {
    
    
        //用i记录重复位置
        int i = 0;
        //128个字符
        int[] temp = new int[129];
        int length = 0;
        for(int k=0;k<s.length();k++){
    
    
            i = Math.max(i,temp[s.charAt(k)]);
            temp[s.charAt(k)]=k+1;
            length = Math.max(length,k-i+1);
        }
        return length;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41454682/article/details/112897655