• Leetcode-3 Longest Substring Without Repeating Characters


    

    1.3- Longest Substring Without Repeating Characters

    Given a string,find the length of the longest substring without repeating characters. Forexample, the longest substring without repeating letters for"abcabcbb" is "abc", which the length is 3. For"bbbbb" the longest substring is "b", with the length of 1.

     

    解法一:

    分析:1.使用一个标志数组boolean flag[256],标识每一个字符是否出现过(注意考虑字符不要仅限于’a,b,c’) (上面样例则使用ArrayList或者HashMap来实现相同的记录功能。明显性能较差)。

    2.acetbglsbcd:这里算法对其进行遍历,遇到反复的字符,记录下当前最大的字符长度;下一循环从该反复字符的第一位置加1k+1)開始就可以


    public static int lengthOfLongestSubstring3(String s) {
    	//注意推断String要注意同一时候推断是否为null及空字符串
          if((s == null)||(s.equals("")))
          {
              return 0;
          }
          //用以记录256个字符都是否出现没
          boolean[] flag = new boolean[256];
          int maxLength = 0;
          int startindex = 0;
          char[] chars = s.toCharArray();
          
          for(int i = 0; i < chars.length; i++)
          {
        	  //推断字符是否已经出现。与HashMap一样都是O(1)
        	  if(flag[chars[i]])
        	  {
        		  //记录最长字符串的长度
        		  maxLength = Math.max(maxLength, i - startindex);
        		  
        		  //更新标志数组的值
        		  //注意这里每次遍历的字符串都会更新,故应该从startindex開始
        		  for(int j = startindex; j < i; j++)
        		  {
        			  if(chars[i] == chars[j])
        			  {
        				  startindex = j + 1;
        				  break;
        			  }
        			  //清除在重服字符出现的字符标志
        			  flag[chars[j]] = false; 
        		  }
        	  }
        	  //未出现情况:注意这个重服出现的字符也要置为真
        	  flag[chars[i]] = true; 
          }
          //考虑一次遍历全然,而没有重服字符的情况
          return Math.max(maxLength, chars.length - startindex);
      }
    
    

    解法二:


    时间复杂度较差的解法:


    使用ArrayList或者HashMap集合实现:

    public static int lengthOfLongestSubstring(String s) {
    		 //注意推断String要注意同一时候推断是否为null及空字符串
            if((s == null)||(s.equals("")))
            {
                return 0;
            }
            int maxLength = 0;
            int tempLength = 0;
            char[] chars = s.toCharArray();
            List<Character> arrayList = new ArrayList<>();
            for(int i = 0; i < chars.length; i++)
            {
                if(!arrayList.contains(chars[i]))
                {
                    arrayList.add(chars[i]);
                    tempLength ++;
                }
                else
                {
                	arrayList.add(chars[i]);
                    maxLength = (maxLength > tempLength) ? maxLength : tempLength;
                    int index = arrayList.indexOf(chars[i]);
                    
                    arrayList = arrayList.subList(index, arrayList.size()-1);
                    /*使用一步一步清除一定要注意每一步remove都是独立的*/
    //                for(int j = 0 ;j <= index; j++)
    //                {
    //                    arrayList.remove(0);//注意这一步
    //                }
                    tempLength = arrayList.size();
                    
                }
            }
            //考虑整条字符串都无反复的情况
            maxLength = (maxLength > tempLength) ? maxLength : tempLength;
            return maxLength;
        }
    
    public static int lengthOfLongestSubstring2(String s) {
    		 //注意推断String要注意同一时候推断是否为null及空字符串
           if((s == null)||(s.equals("")))
           {
               return 0;
           }
           int maxLength = 0;
           int startindex = 0;
           int tempLength = 0;
           char[] chars = s.toCharArray();
           
           HashMap<Character, Integer> map = new HashMap<>();
           
           for(int i = 0; i < chars.length; i++)
           {
        	   
        	   if(map.containsKey(chars[i]))
        	   {
        		   maxLength = (maxLength > tempLength) ?

    maxLength : tempLength; int index = map.get(chars[i]); for(int j = startindex; j <= index; j++) { map.remove(chars[j]); } startindex = index; } map.put(chars[i], i - startindex); tempLength = map.size(); } //考虑整条字符串都无反复的情况 maxLength = (maxLength > tempLength) ? maxLength : tempLength; return maxLength; }





    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    牛客多校(2020第九场)I The Crime-solving Plan of Groundhog
    牛客多校(2020第九场)I The Crime-solving Plan of Groundhog
    杭电多校第六场 1006 A Very Easy Graph Problem(最小生成树) + Krusal算法的简介
    杭电多校第六场 1006 A Very Easy Graph Problem(最小生成树) + Krusal算法的简介
    牛客多校(2020第八场)G-Game SET
    牛客多校(2020第八场)G-Game SET
    牛客多校(2020第八场)I
    牛客多校(2020第八场)I
    阿里云 Windows系统连接linux putty access denied
    跨域问题
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4852190.html
Copyright © 2020-2023  润新知