3. 无重复字符的最长子串
题目
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
使用Hashmap + 移动窗口
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map = new HashMap<>();
int left = 0;
int cur = 0;
int result = 0;
for(int i = 0;i < s.length();i++){
if(map.containsKey(s.charAt(i))){
left = Math.max(left,map.get(s.charAt(i)) + 1);
}
cur = i - left + 1;
result = Math.max(cur,result);
map.put(s.charAt(i),i);
}
return result;
}
}