最长不重复子串长度,时间复杂度O(n),空间复杂度O(n),Python实现

 

def lengthOfLongestSubstring(s):
    res = 0
    d = {}
    tmp = 0
    start = 0
    for i in range(len(s)):
        if s[i] in d and d[s[i]] >= start:
            start = d[s[i]] + 1
        tmp = i - start + 1
        d[s[i]] = i
        res = max(res, tmp)
    return res

print(lengthOfLongestSubstring("abcdbe"))
# 4

 

posted @ 2018-01-10 16:29  黎明程序员  阅读(245)  评论(0编辑  收藏  举报