-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution003.java
66 lines (57 loc) · 1.65 KB
/
Solution003.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package algorithm.leetcode;
import java.util.*;
/**
* @author: mayuan
* @desc: 无重复字符的最长子串
* @date: 2018/06/24
*/
public class Solution003 {
/**
* 使用滑动窗口方式解题
*
* @param s
* @return
*/
public int lengthOfLongestSubstring(String s) {
int sLength = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(sLength);
for (int j = 0, i = 0; j < sLength; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(i, map.get(s.charAt(j)));
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
public int lengthOfLongestSubstring2(String s) {
int ans = 0;
Map<Character, Integer> map = new HashMap<>();
for (int start = 0, end = 0; end < s.length(); ++end) {
char c = s.charAt(end);
if (map.containsKey(c)) {
// 起始指针只能向后移动
start = Math.max(map.get(c) + 1, start);
}
ans = Math.max(ans, end - start + 1);
map.put(s.charAt(end), end);
}
return ans;
}
/**
* 采用滑动窗口的方式,求解问题
* [i, j) 左闭右开区间
* @param s
* @return
*/
public int lengthOfLongestSubstring3(String s) {
int[] index = new int[128];
int ans = 0;
for (int j = 0, i = 0; j < s.length(); ++j, ++i) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
}