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

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

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

全部评论

相关推荐

08-27 21:03
已编辑
西南石油大学 Java
冷花幽露:大概率是了,京东面试就是这样。我上周一面也是20多分钟,面试官问的很刁钻的问题也答上来了,面完过了几天还是没推进,泡池子,昨天一看挂了。如果一面完第2天没有收到2面邀请,基本上不用抱希望了。如果你的bg是985,面试流程也是和我们一样,20多分钟,唯一区别就是面完他们会很快收到二面邮件,而不像我们泡池子然后挂掉
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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