forked from liuyubobobo/Play-with-Algorithm-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution5.java
42 lines (32 loc) · 1.43 KB
/
Solution5.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
// 3. Longest Substring Without Repeating Characters
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
//
// 滑动窗口
// 其中使用last[c]保存字符c上一次出现的位置, 用于在右边界发现重复字符时, 快速移动左边界
// 使用这种方法, 时间复杂度依然为O(n), 但是只需要动r指针, 实际上对整个s只遍历了一次
// 相较而言, 之前的方法, 需要移动l和r两个指针, 相对于对s遍历了两次
import java.util.Arrays;
// 时间复杂度: O(len(s))
// 空间复杂度: O(len(charset))
public class Solution5 {
public int lengthOfLongestSubstring(String s) {
int[] last = new int[256];
Arrays.fill(last, -1);
int l = 0, r = -1; //滑动窗口为s[l...r]
int res = 0;
while(r + 1 < s.length()){
r ++;
if(last[s.charAt(r)] != -1)
l = Math.max(l, last[s.charAt(r)] + 1);
res = Math.max(res, r - l + 1);
last[s.charAt(r)] = r;
}
return res;
}
public static void main(String[] args) {
System.out.println((new Solution5()).lengthOfLongestSubstring( "abcabcbb" ));
System.out.println((new Solution5()).lengthOfLongestSubstring( "bbbbb" ));
System.out.println((new Solution5()).lengthOfLongestSubstring( "pwwkew" ));
System.out.println((new Solution5()).lengthOfLongestSubstring( "" ));
}
}