LCR 016. 无重复字符的最长子串
解题思路
- 窗口内的字符串就是不重复子串
- 每次遇到新的字符 看看窗口内是否存在该字符 如果存在直接剔除 然后调整窗口左边界
- 不存在 添加窗口内部 右边界++
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() <= 1){
return s.length();
}
int res = 1;
int left = 0;
int right = 0;
Set<Character> window = new HashSet<>();
while(right < s.length()){
char ch = s.charAt(right);
while(window.contains(ch)){
window.remove(s.charAt(left));
left++;
}
res = Math.max(res,right - left + 1);
window.add(ch);
right++;
}
return res;
}
}
- 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