Python 实现LeetCode刷题之无重复字符的最长子串

class Solution(object):
    def LengthOfLongestSubstring(self,s):
        d = {}
        start = 0
        ans = 0
        for i,c in enumerate(s):
            if c in d:
                start = max(start, d[c] + 1)
            d[c] = i
            ans = max(ans, i-start + 1)
        return ans


if __name__ == '__main__':
    s = 'pwwkew'
    a = Solution()
    res = a.LengthOfLongestSubstring(s)
    print(res)

 

你可能感兴趣的:(python)