Leetcode最长子串问题

  • 动态规划局部练习
def findMax(target: str):
    '''
    type target: "aksfkjsdghguiqebgbrbgipqge"
    rtype : 最长的个数
    '''
    tmp_con = ""
    last_num = target[-1]
    l = len(target)
    while l:
        if target[l-1] not in tmp_con:
            tmp_con += target[l-1]
            l -= 1
        else:
            return len(tmp_con)
            break
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if s == '':
            return 0
        if len(s) == 1:
            return 1

        def find_left(s, i):
            tmp_str = s[i]
            j = i - 1
            while j >= 0 and s[j] not in tmp_str:
                tmp_str += s[j]
                j -= 1
            return len(tmp_str)
        length = 0
        for i in range(0, len(s)):
            length = max(length, find_left(s, i))
        return length

作者:marcusxu
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/python3ti-jie-chao-jian-dan-de-dong-tai-gui-hua-ji/
来源:力扣(LeetCode)
发布了140 篇原创文章 · 获赞 53 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44291044/article/details/105178599