-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.java
More file actions
50 lines (42 loc) · 920 Bytes
/
Solution2.java
File metadata and controls
50 lines (42 loc) · 920 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
/**
* code by : 老王
* https://github.com/simplemain
*/
import java.util.Set;
import java.util.HashSet;
public class Solution2
{
public static void main(String[] args)
{
final String s = "abcabcbb";
final Solution sol = new Solution2().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;
for (int p = 0; p < len; p++)
{
final Set<Character> exists = new HashSet<>();
exists.add(cs[p]);
for (int q = p + 1; q < len; q++)
{
final boolean isOK = !exists.contains(cs[q]);
if (!isOK) break;
exists.add(cs[q]);
}
final int curLen = exists.size();
if (curLen > maxLen)
{
maxLen = curLen;
}
}
return maxLen;
}
}
}