方法1:暴力法
方法2:滑动窗口
用set保存已存在的字符,如果end处的字符重复,就将start减1
class Solution {
public int lengthOfLongestSubstring(String s) {
int start = 0;
int end = 0;
int res = 0;
Set<Character> set = new HashSet<>();
while(end<s.length()){
if(set.contains(s.charAt(end))){
set.remove(s.charAt(start));
start++;
}else{
set.add(s.charAt(end));
end++;
res = Math.max(res,end-start);
}
}
return res;
}
}
方法3:动态规划
原文:https://www.cnblogs.com/ermiao-zy/p/14791232.html