【LeetCode】 3. 无重复字符的最长子串

题目描述:(中等难度)

【LeetCode】 3. 无重复字符的最长子串_第1张图片

解题思路

双指针

Python 代码

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        n = len(s)
        ans = 0
        dic = {}
        start = 0
        end = 0
        while end < n:
            alpha = s[end]
            if alpha in dic:
                start = max(dic[alpha], start)
            ans = max(ans, end - start + 1)
            dic[alpha] = end + 1
            end += 1
        return ans

s = Solution()
res = s.lengthOfLongestSubstring("pwwkew")
print(res)

你可能感兴趣的:(数据结构与算法)