leetcood学习笔记-3-无重复字符的最长子串

题目描述:

方法一:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        slow = 0
        fast = 0
        res_max = 0
        table = dict()
        while slow<len(s) and fast<len(s):
            if s[fast] in table:
                del(table[s[slow]])
                slow+=1
            else:
                table[s[fast]] = fast
                fast+=1
                res_max = max(res_max,fast-slow)
        return res_max

优化:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int: # 定义变量存储子字符串 sub_str = "" # 存储最大长度 max_length = 0 # 遍历字符串 for i in range(len(s)): if s[i] in sub_str: # 如果第i个字符在子字符串中已经存在,删除包含重复字符及之前的所有字符 sub_str = sub_str.split(s[i])[1] # 追加第i个字符到子字符串中 sub_str += s[i] if len(sub_str) > max_length: max_length = len(sub_str) return max_length

 

转载于:https://www.cnblogs.com/oldby/p/11152712.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值