leetcode刷題,沒想到這麼難搞!

leetcode以前提交的代碼不見了,因此在這裏記一下
https://leetcode.com/problems/longest-substring-without-repeating-characters/code

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        rtv = start = current = 0

        last_seen = {}
        while current < len(s):
            char = s[current]
            if char in last_seen:
                start = max(start, last_seen[char] + 1)
            if current - start + 1 > rtv:
                rtv = current - start + 1
            last_seen[char] = current
            current += 1
        return rtv
相關文章
相關標籤/搜索