找出字符串中不重复字符的最长子串的长度

时间复杂度为O(1)

采用双指针方法

public class StrTest {
    
       public int  getMax(int a , int b){
            return a>b?a:b;
        }
        
        public  int lengthOfLongestSubstring(String str){
            int max = 0; 
            int i = 0;
            int j = 0;
            int len = str.length();
            String sub="";
            while(i<len && j <len){
                int temp = sub.indexOf(str.charAt(j));
                if(temp == -1){
                     //不存在就追加到子串的末尾
                    //窗口往右移动一格
                    sub +=str.charAt(j);
                    j++;
                    max = getMax(max, j-i);
                }else{
                    //存在就把子串的第一个字符截取掉,保留剩下的,
                    //i++  窗口往右移动一格
                    sub = sub.substring(1, sub.length());
                    i++;
                }
            }
            return max;
        }

        public static void main(String[] args) {
            int length = new StrTest().lengthOfLongestSubstring("aabbcdevbbdd");
            System.out.println(length);
            
        }
    
}
原文地址:https://www.cnblogs.com/moris5013/p/9461498.html