Leetcodeブラッシングレコード-3。繰り返し文字のない最長の部分文字列

ここに画像の説明を挿入

私が実装した方法:スライディングウィンドウメソッド:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:

        
        start = 0
        cur = 0
        length = len(s)
        if length == 0:
            return 0
        elif length == 1:
            return 1
        cur_res = ''
        tempdict = {}
        rem_res = ''
        rem_len = 1
        while cur != length:
            #if len(tempdict) == 26:
            #    return cur_res
            if s[cur] not in tempdict:
                tempdict[s[cur]] = cur
                cur_res += s[cur]
                cur += 1
            elif s[cur] in tempdict:
                temp_len = cur - start
                if temp_len > rem_len:
                    rem_res = s[start:cur]
                    rem_len = temp_len
                start = tempdict[s[cur]]+1
                tempdict = {}
                tempdict[s[start]] = start
                cur = start + 1
        
        if start == 0:
            return length
        if cur - start > rem_len and s[start] != s[cur-1]:
            return (cur - start )
        return rem_len
            
43件の元の記事を公開 14 件を賞賛・2 万回以上の閲覧

おすすめ

転載: blog.csdn.net/weixin_41545780/article/details/104935129