当前位置:网站首页>[leetcode] sword finger offer II 016 Longest substring without duplicate characters

[leetcode] sword finger offer II 016 Longest substring without duplicate characters

2022-06-10 01:32:00 LawsonAbs

0. summary

  • It is very important to be familiar with the functions commonly used in language , This can help quickly improve the speed and quality of the code
  • Blog source : [email protected]

1. subject

2. thought

The idea of double pointers . Follow 【 The sliding window 】 It's like ?

3. Code

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        span = []
        res = 0
        left = right = 0
        for right in range(len(s)):
            if s[right] not in span:
                span.append(s[right])
                res = max(res,len(span))
            else:
                while(s[right] in span):
                    span.remove(s[left])
                    left+=1
                span.append(s[right])
        return res


原网站

版权声明
本文为[LawsonAbs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100107226170.html