Longest Substring Without Repeating Characters

 

Given a string, find the length of the longest substring without repeating characters. For example, 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.

采用的是比较笨的遍历方法,time=380ms  可通过,不过还要优化

public class Solution {
    public int lengthOfLongestSubstring(String s) {
	        int length=s.length();
	        int max=0,index=0,count=1;
	        
	        while(index<length){
	        	if(index+count>=length){
		             if(max<=count){
		                  max=count;
		                  count=1;
		             }
		             break;		                
		        }
	            for(int i=index;i<index+count;i++){	            	
	                if(s.charAt(index+count)==s.charAt(i)){
	                    index=i+1;
	                    if(max<=count){
	                        max=count;
	                    }
	                    count=0;
	                    break;
	                }
	            }
	            count++;
	        }
	        return max;
	        
	    }
}


相关文章:

  • 2021-08-01
  • 2022-02-13
  • 2021-08-03
  • 2022-01-06
猜你喜欢
  • 2022-12-23
  • 2021-11-14
  • 2021-12-30
  • 2021-09-13
  • 2021-09-11
  • 2021-08-28
  • 2021-10-04
相关资源
相似解决方案