forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution3.java
40 lines (31 loc) · 1.27 KB
/
Solution3.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
// 3. Longest Substring Without Repeating Characters
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
//
// 滑动窗口的另一个实现, 仅做参考
// 时间复杂度: O(len(s))
// 空间复杂度: O(len(charset))
public class Solution3 {
public int lengthOfLongestSubstring(String s) {
int[] freq = new int[256];
int l = 0, r = -1; //滑动窗口为s[l...r]
int res = 0;
while(r + 1 < s.length()){
while(r + 1 < s.length() && freq[s.charAt(r+1)] == 0)
freq[s.charAt(++r)] ++;
res = Math.max(res, r - l + 1);
if(r + 1 < s.length()){
freq[s.charAt(++r)] ++;
assert(freq[s.charAt(r)] == 2);
while(l <= r && freq[s.charAt(r)] == 2)
freq[s.charAt(l++)] --;
}
}
return res;
}
public static void main(String[] args) {
System.out.println((new Solution3()).lengthOfLongestSubstring( "abcabcbb" ));
System.out.println((new Solution3()).lengthOfLongestSubstring( "bbbbb" ));
System.out.println((new Solution3()).lengthOfLongestSubstring( "pwwkew" ));
System.out.println((new Solution3()).lengthOfLongestSubstring( "" ));
}
}