题3、 Longest Substring Without Repeating Characters

题3、 Longest Substring Without Repeating Characters

topic

Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:

Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

Thinking

This question is to let everyone find a string inside, do not repeat the longest substring, including itself.

Start idea is to use a direct function index () where a direct access to the same letter, and the maximum value is obtained by subtracting the direct result. But it seems to be wrong, there is nothing to big brother, then you can go to try, anyway, I tried it did not succeed (as if fraught).

After the discovery of the string points still have to cut different lengths, then one by one judge, and finally get the final result, because he is to get the longest string length, so I started (the longest input from a string which string) in descending order.

It should be noted that this method for too long, it can also be a string, long dead run computer ran out, anyway, I ran for an hour did not result, will enter 3300 characters. After the complexity of the issues of the time bald head, finally had an idea, string it stands to reason that a total of 128 (the title of the exact number I do not know, anyway, I came in accordance with the ASCII code), so a string of non-repetition the maximum length of it 128, which greatly reduces the number of cycles.

Last is the boundary value problem and spaces, each handling special mechanisms, test it directly, mindless tired.

Code

This time things too hard, and there is a comment

public class T003 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println( lengthOfLongestSubstring("asdasd") );
	}
	
    public static int lengthOfLongestSubstring(String s) {
    	
    	int sl = s.length();
    	int tl = 0;
    	
    	//如果字符串长度小于等于1,肯定不重复。直接返回结果0/1
    	if( sl <= 1 )
    		return sl;
    	
    	//第一个循环,i每加一,子串的长度就减少一,从零开始,也就是说子串的长度从s.length()开始
    	for( int i = 0; i<sl; i++ ) {
    		
    		//用来切割字符串S,得到子串,子串每减少1,那么切割得到的子串个数就加一
    		//其中对子串长度进行了限制,最大128也就是(sl>128 ? sl-127 : i+1)这一行代码
    		for( int j = 0; j < (sl>128 ? sl-127 : i+1); j++ ) {
    			
    			//切割,从而得到子串
    			String temp = s.substring( j, j+(sl>128 ? 128 : sl)-i );
    			//System.out.println( i + "\t" + temp );
    			tl = temp.length();
    			
    			//用来判断是不是要得到的子串
    			for( int k = 0; k<tl; k++ ) {
    				
    				//如果出现重复的就跳出循环
    				if( temp.indexOf(temp.charAt(k), k+1) != -1 )
    					break;
    				
    				//循环到了最后一次了,没问题,那可不就是咱要输出的
    				if( k == tl-1 ) {
    					//System.out.println("\n\n"+ temp + "\t" + sl );
    					return tl;
    				    
    				}
    			}
    		}
    	}
 		return 0;
   
    }
}
Published 25 original articles · won praise 0 · Views 131

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/103465889
Recommended