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

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

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

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @return int整型
#
class Solution:
    def lengthOfLongestSubstring(self , s: str) -> int:
        # write code here
        dp = [1 for i in range(len(s))]
        dic = {}
        dic[s[0]] = 0
        left = 0
        right = 0
        resl = 0
        resr = 0
        for j in range(1,len((s))):
            if s[j] not in dic.keys():
                right = j
                dic[s[j]]=j
                dp[j] = dp[j-1]+1
                if right-left>resr-resl:
                    resl = left
                    resr = right
            else:
                orileft = left
                oriplace = dic[s[j]]
                left = dic[s[j]]+1               
                for k in range(orileft,left,1):
                    del dic[s[k]]
                dic[s[j]]=j
                right = j
                dp[j]= right-left+1
                if right-left>resr-resl:
                    resl = left
                    resr = right
        
        return resr-resl+1

全部评论

相关推荐

07-03 16:02
门头沟学院 Java
今天面试,非常紧张,面试官问我springboot有哪些核心模块都答不上来了,真的对自己无语了!
程序员小白条:28届我勒个去,很多人面试都没机会
查看1道真题和解析
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:13
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务