https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
public class L_00003_LengthOfLongestSubstring {
public int lengthOfLongestSubstring1(String s) {
if (s == null) {
return 0;
}
Set<Character> charSet = new HashSet<>();
int result = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
int curLen = 0;
for (int j = i; j < len; j++) {
if (charSet.contains(s.charAt(j))) {
break;
}
charSet.add(s.charAt(j));
curLen++;
}
charSet.clear();
result = Math.max(result, curLen);
}
return result;
}
public int lengthOfLongestSubstring2(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
// try to extend the range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
public int lengthOfLongestSubstring3(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>();
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)) + 1, i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j);
}
return ans;
}
}
lengthOfLongestSubstring2是常规的滑窗方式:
| a b c d e c o p s
| a b | c d e c o p s
| a b c | d e c o p s
| a b c d | e c o p s
| a b c d e | c o p s
| a b c d e c | o p s :出现重复了,右窗不动,左窗平移
a | b c d e c | o p s
a b | c d e c | o p s
a b c | d e c | o p s :直到重复值被移出窗口
a b c | d e c o | p s
a b c | d e c o p | s
a b c | d e c o p s | :结果为6
lengthOfLongestSubstring3对上述进行优化,遇到重复值没必要一个一个移动,左窗可以直接跳到str[重复值+1]的位置。
实现使用MAP记录每个值的位置,如果发现重复,让left_index直接跳到重复值位置+1。
i = Math.max(map.get(s.charAt(j)) + 1, i);
左窗口索引要保证只能向右滑动:
| a b c b a d 0
| a | b c b a d 1
| a b | c b a d 2
| a b c | b a d 3
| a b c b | a d 3
a b | c b | a d 3 又遇到a了,如果使用a的索引值左窗就又滑回最左侧
a b | c b a | d 3
a b | c b a d | 4
lengthOfLongestSubstring2:O(2n)
lengthOfLongestSubstring3:O(n)