zoukankan      html  css  js  c++  java
  • 找出字符串中不重复字符的最长子串的长度

    时间复杂度为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);
                
            }
        
    }
  • 相关阅读:
    暑假自学(19)
    暑假自学(18)
    暑假自学(17)
    暑假自学(16)
    暑假自学(15)
    rsync简介
    10.Go语言-面向对象简单了解
    dp 数字三角形
    P1359租用游艇(dp+dfs)
    油田问题 bfs
  • 原文地址:https://www.cnblogs.com/moris5013/p/9461498.html
Copyright © 2011-2022 走看看