首页 > 编程语言 > 详细

leetcode-python-无重复字符的最长子串

时间:2021-06-14 17:13:51      阅读:18      评论:0      收藏:0      [点我收藏+]

1)双指针,若fast不存在temp中,则加入。若存在则删除首位,slow前进一位。

保留一个临时变量保存最大长度。

时间复杂度O(n)

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        length = len(s)
        if length == 0:
            return 0
        if length == 1:
            return 1
        result = list()
        slow = 0
        fast = 1
        maxlength = 0
        temp = s[slow]
        while slow <length and fast < length:
            
            if s[fast] not in temp:
                temp += s[fast]
                fast += 1
            else:
                slow += 1
                temp =s[slow:fast]
            maxlength = max(maxlength, len(temp))
        return maxlength
技术分享图片

 

 

 

leetcode-python-无重复字符的最长子串

原文:https://www.cnblogs.com/cbachen/p/14882611.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!