Leetcode-003-无重复字符最长子串长度

动态规划初级题,或者理解为滑动窗口。

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

 

上一篇:L1-003 个位数统计


下一篇:L1-003 个位数统计