Leetcode Medium 3 Longest Substring Without Repeating Characters

用滑动窗口 

class Solution:
    def lengthOfLongestSubstring(self, s):
        i, j, ans = 0, 0, 0
        len_s = len(s)
        max_str = ''
        while j < len_s:
            if not s[j] in max_str:
                max_str += s[j]
                j += 1
                ans = max(ans, j-i)
            else:
                max_str = max_str[1:]
                i += 1
        return ans

猜你喜欢

转载自blog.csdn.net/li_k_y/article/details/86444342