题解 | #最长不含重复字符的子字符串#

最长不含重复字符的子字符串

http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

使用队列的方法,将字符串中的字符依次放入队列中,放入前判断队列中是否已经存在该值.若存在该值,计算当前的最大长度。然后队列出的方式不断将元素排出直到已经存在的那个元素被排出。

# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def lengthOfLongestSubstring(self , s: str) -> int:
        # write code here
        if not s:
            return 0
        aqueue = []
        res = 0
        for ind, val in enumerate(s):
            if val not in aqueue:
                aqueue.append(val)
            else:
                res = res if res > len(aqueue) else len(aqueue)
                temp = aqueue.pop(0)
                while temp != val:
                    temp = aqueue.pop(0)
                aqueue.append(val)

        res = res if res > len(aqueue) else len(aqueue)

        return res
全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务