zoukankan      html  css  js  c++  java
  • 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; }





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

  • 相关阅读:
    【第五周读书笔记】我是一只IT小小鸟
    【第三周读书笔记】浅谈node.js中的异步回调和用jsxlsx操作Excel表格
    【第四周读书笔记】读构建之法第11到第16章
    【第一次个人作业】一条咸鱼的词频统计
    win10连接无线网,开启移动热点,手机连接它手机一直显示获取ip地址中。
    每月一次,免费领取小米云服务会员
    Spring Day 1
    oracle闪回查询和闪回数据库
    oracle异库同表名的两个数据库中数据合并或数据表结构的修改
    oracle使用SQL来生成SQL
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4852190.html
Copyright © 2011-2022 走看看