無重複字符的最長子串-python

 

def lengthOfLongestSubstring(self, s):
    """
    :type s: str
    :rtype: int
    """
    if not s:
        return 0
    temp = set()
    n = len(s)
    count = 0
    left = 0
    res = 0
    for i in range(n):
        while s[i] in temp:  # 不用列表,集合的實現是基於哈希表的,所以集合的查找速度比列表快
            temp.remove(s[left])
            left += 1
            count -= 1
        temp.add(s[i])
        count += 1
        if res < count:
            res = count

    return res

temp 用列表的查找速度會很慢,這與它的底層實現有關係。

參照https://blog.csdn.net/weixin_37720172/article/details/78636314

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章