leetcode_3_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.

思路:

本题要求的是字符串的非重复最长子串,为了在最长的时间内判断子串是否有重复,很自然地就想到了HashSet(当然还有更好的方法,我女朋友就用数组进行判重),从头开始遍历直到出现重复的字符为止,将非重复的字符数目存储起来,并查出从开始处到重复字符第一次出现的地方,删去HashSet中从开始到重复字符第一次出现的地方的所有元素,并将count减去从开始处至重复字符第一次出现的地方的个数,然后从出现重复字符的下一个字符开始统计。将字符串遍历一遍,然后统计出最长的子串所含有的字符个数即可。

这题最容易想的就是从每个字符开始统计最长非重复连续子串的字符个数,但是很显然,时间复杂度太高。然后想到了从出现重复字符的地方开始,很显然是不对的,这题想了好久,脑子都要糊了有木有^-^!。最后费了好大的劲,才想清楚,应该从重复字符第一次出现的地方开始,这又涉及到HashSet中元素的删除,count减去从开始处到重复字符第一次出现的地方的字符个数,很多。结果,我女朋友生生地用数组就解决了,很简单的那种!晚上回去也没写程序,但心里有谱了,第二天早上到实验室一会写出来了,尽管还是很复杂!Anyway,It‘s
done!

写程序这种事情,脑子不清楚的时候,最后不要做,否则你会后悔的,欲罢不能有做不出来!!!

代码:

public int lengthOfLongestSubstring(String s) {
		if(s.equals(""))
			return 0;
	   int len=s.length();
       int max=0;
       int arr[]=new int[len];
       Arrays.fill(arr, 0);
       HashSet<Character>set=new HashSet<Character>();
       int count=0,j=0;
       char ch;
       int temp=0;
       for(int i=0;i<len;i++)
       {
    	   j=i;
    	   while(j<len)
    	   {
    		   ch=s.charAt(j);
    		   if(!set.contains(ch))
        	   {
        		   set.add(ch);
        		   count++;
        		   j++;
        	   }
        	   else
        	   {
        		   arr[i]=count;
        		   int end=s.indexOf(ch, temp);
        		   for(int k=temp;k<end;k++)
        			   set.remove(s.charAt(k));
        		   count-=(end-temp);
        		   i=j;
        		   temp=end+1;
        		   break;
    		   }
    	   }
    	   if(j==len)
    	   {
    		   arr[i]=count;
    		   break;
    	   }
       }
       max=arr[0];
       for(int i=1;i<len;i++)
    	   if(max<arr[i])
    		   max=arr[i];
       return max;
    }

结果:

时间: 2024-09-27 00:49:55

leetcode_3_Longest Substring Without Repeating Characters的相关文章

leetcode4 ---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 subst

LeetCode---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 subst

LeetCode: 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 subst

LeetCode3 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 s

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode Problem 3.Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode 3_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

Leetcode 03 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 subst

Java for LeetCode 003 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 subst