[leetcode]python3 算法攻略-无重复字符的最长子串

给定一个字符串,找出不含有重复字符的最长子串的长度。

方案一:找到规律即可。

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        res = ''
        maxlen = 0
        for ch in s:
            if ch not in res:
                res += ch
            else:
                maxlen = max(maxlen, len(res))
                index = res.index(ch)
                res = res[index + 1:] + ch
        return max(maxlen, len(res))
        

猜你喜欢

转载自blog.csdn.net/zhenghaitian/article/details/81257243