标签:while 子串 dem 动态规划 lse self substr sel div
动态规划初级题,或者理解为滑动窗口。
class Solution { public int lengthOfLongestSubstring(String s) { Set<Character> demo = new HashSet<>(); int l=0,r=0,result=0; while(l<s.length()&&r<s.length()){ if(demo.contains(s.charAt(r))){ demo.remove(s.charAt(l)); l++; }else{ demo.add(s.charAt(r)); r++; } if(result<r-l)result = r-l; } return result; } }
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: l=0 r=0 result=0 demo = set() while(l<len(s) and r<len(s)): if s[r] in demo: demo.remove(s[l]) l+=1 else: demo.add(s[r]) r+=1 if result < r-l: result = r-l return result
标签:while 子串 dem 动态规划 lse self substr sel div
原文地址:https://www.cnblogs.com/huangzengrui/p/12336914.html