-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
36 lines (34 loc) · 1.01 KB
/
Solution.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
package _003;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/11
* desc :
* </pre>
*/
public class Solution {
public int lengthOfLongestSubstring(String s) {
int len;
if (s == null || (len = s.length()) == 0) return 0;
int preP = 0, max = 0;
int[] hash = new int[128];
for (int i = 0; i < len; ++i) {
char c = s.charAt(i);
if (hash[c] > preP) {
preP = hash[c];
}
int l = i - preP + 1;
hash[c] = i + 1;
if (l > max) max = l;
}
return max;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.lengthOfLongestSubstring("abcabcbb"));
System.out.println(solution.lengthOfLongestSubstring("bbbbb"));
System.out.println(solution.lengthOfLongestSubstring("pwwkew"));
System.out.println(solution.lengthOfLongestSubstring("Abcabcbb"));
}
}