-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongSubNoRepeat.java
45 lines (45 loc) · 1.18 KB
/
LongSubNoRepeat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.HashSet;
/**
* 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
* @author sinlly
*
*/
public class LongSubNoRepeat {
public int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==0)
return 0;
HashSet<Character> tmpSet=new HashSet<Character>();
int max=0;
int right=0;//ÓÒ±ßË÷Òý
int left=0;//×óË÷Òý
while(right<s.length())
{
if(!tmpSet.contains(s.charAt(right)))
{
tmpSet.add(s.charAt(right));
}
else
{
if(right-left>max)
max=right-left;
while(s.charAt(right)!=s.charAt(left))
{
tmpSet.remove(s.charAt(left));
left=left+1;
}
left=left+1;
}
right=right+1;
}
if(max<right-left)
max=right-left;
return max;
}
public static void main(String[] args)
{
LongSubNoRepeat opt=new LongSubNoRepeat();
System.out.println(opt.lengthOfLongestSubstring("abcdefab"));
}
}