longest-substring-without-repeating-characters.py

longest-substring-without-repeating-characters.py

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

https://leetcode.cn/problems/longest-substring-without-repeating-characters/

结果: ⋆ ⋆ ⋆ \star\star\star

20220612
> https://leetcode.cn/problems/longest-substring-without-repeating-characters/

执行用时:60 ms, 在所有 Python3 提交中击败了84.22%的用户
内存消耗:15 MB, 在所有 Python3 提交中击败了96.26%的用户

time cost: 10 min

代码:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        max_l = 0
        sub_s = ''
        for str in s:
            idx = sub_s.find(str)
            if idx != -1:
                sub_s = sub_s.split(str)[1]
            sub_s += str
            temp_l = len(sub_s)
            if temp_l > max_l:
                max_l = temp_l
            # print(sub_s)
        return max_l

思路

遍历序列 s s s的每个字符 s t r str str

初始化子序列 s u b _ s sub\_s sub_s,如果在 s u b _ s sub\_s sub_s中能找到 s t r str str,则说明该阶段的子序列查找完毕

s t r str str为标志分割 s u b _ s sub\_s sub_s,取后面的取代 s u b _ s sub\_s sub_s

key: 这里截取后的 s u b _ s sub\_s sub_s要加上 s t r str str,不然会漏掉这个字符

遍历完 s s s结束程序

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值