3. Longest Substring Without Repeating Characters

 

#import itertools as it
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        mlen=0
        n=len(s)
        for i in range(n):
            zd={} #字典比list运算快许多
            for ele in s[i:]:
                if ele not in zd:
                    zd[ele]=1
                else:
                    break
            length=len(zd)
            #print(length)
            if length >mlen:
                mlen=length
        return mlen
            
        
        


class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        output=0
        track=''#用str的运算速度也比list快
        
        for c in s:
            if c in track:
                while True:        ####从左边开始判断重复,确定新的起点 
                    if  track[0]==c:
                        track=track[1:]
                        track+=c
                        break
                    else:
                        track=track[1:]
            else:
                track+=c
            
            if len(track)>output:
                output=len(track)
        return output
        
  

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        track, output, start = {}, 0, 0
        for i, v in enumerate(s):
            if v not in track or track[v] < start: #v不在track中或者在开始计数的字符之前
                output = max(output, i - start + 1)
            else:
                start = track[v] + 1 #更新开始计数的初始字符位置
            track[v] = i
        return output

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值