-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution4.java
More file actions
58 lines (50 loc) · 981 Bytes
/
Solution4.java
File metadata and controls
58 lines (50 loc) · 981 Bytes
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
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* code by : 老王
* https://github.com/simplemain
*/
public class Solution4
{
public static void main(String[] args)
{
final String s = "abcabcbb";
final Solution sol = new Solution4().new Solution();
final int result = sol.lengthOfLongestSubstring(s);
System.out.printf("%d\n", result);
}
class Solution
{
public int lengthOfLongestSubstring(String s)
{
final int len = s.length();
final char[] cs = s.toCharArray();
int maxLen = 0;
final boolean[] exists = new boolean[256];
int curLen = 0;
int left = 0;
for (int right = 0; right < len; right++)
{
final char c = cs[right];
if (exists[c])
{
for (int j = left; j < right; j++)
{
exists[cs[j]] = false;
curLen--;
if (cs[j] == c)
{
left = j + 1;
break;
}
}
}
exists[c] = true;
curLen++;
if (curLen > maxLen)
{
maxLen = curLen;
}
}
return maxLen;
}
}
}