题解 | #最长不含重复字符的子字符串#

最长不含重复字符的子字符串

https://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def lengthOfLongestSubstring(self , s: str) -> int:
        # write code here
        result=[0 for i in range(len(s))]
        recordset=set()
        templen=0
        for idx,item in enumerate(s):
            if item not in recordset:# 如果不在集合里说明没有重复的
                recordset.add(item)
                templen+=1
                result[idx]=templen
            else:# 在集合里面
                #找到前面重复的位置
                
                presame=s.index(item,idx-result[idx-1],idx)
                #计算长度
                templen=idx-presame
                recordset=set(s[presame+1:idx+1])
                
                result[idx]=templen
        
        return max(result)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务