2019-03-26 LeetCode 3.无重复字符的最长子串

每一个往后面数,找到第一个重复的

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        tmp_set=set()
        result=0
        for i,e in enumerate(s):
            tmp_set.clear()
            tmp_len=0;
            for index in range(len(s)-i):
                current=s[i+index]
                if(current not in tmp_set):
                    tmp_set.add(current)
                    tmp_len=index+1; # 这个表示到了头,特殊情况,所以index+1
                else:
                    tmp_len=index; # 这是后一个,所以index+1-1
                    break;
            result=max(result,tmp_len)
        return result
            

使用滑动窗口,减少一些搜索空间

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        tmp_set=set()
        i,j,ans=0,0,0
        t=len(s)
        while(i

第二次写滑动窗口,花费15分钟

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        tmp_set=set()
        left,right,ans=0,0,0
        Len=len(s)
        while(right

使用键值对,改进滑动窗口每次只前进一点点的情况,花费20分钟

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        tmp={}
        left,right,ans=0,0,0
         
        while(right

你可能感兴趣的:(2019-03-26 LeetCode 3.无重复字符的最长子串)