【leetcode】3. Longest Substring Without Repeating Characters无重复字符的最长子串

题目描述

思路

查找无重复的字符子串,然后滑动窗口

初次解

每次滑动一格窗口

class Solution:
    def isUnique(self, s: str) -> bool:
        for ch in s:
            if s.count(ch) > 1:
                return False
            else:
                continue
        return True
    def lengthOfLongestSubstring(self, s: str) -> int:
        i,j,Max=0,0,0
        j+=1
        while j <= len(s):
            if self.isUnique(s[i:j]):
                print(s[i:j],"is Unique",i,j)
                Max=max(j-i,Max)
                j+=1
            else:
                i+=1
        return Max

成果

第一次优化

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if(len(s)==1): 
            return 1
        i,j,Max=0,0,0
        while j <= len(s):
            st = s[i:j+1]
            if(j+1 < len(s)):
                index = st.find(s[j+1])
                if index > -1:
                    i+=(index+1)
                j+=1
                Max=max(j-i+1,Max)
            else:
                break
        return Max

成果

2020-01-15T07:27:31.png

版权声明: (https://www.thinkmoon.cn/post/721)
本文首发于指尖魔法屋-【leetcode】3. Longest Substring Without Repeating Characters无重复字符的最长子串
转载或引用必须申明原指尖魔法屋来源及源地址!