149 Longest Substring Without Repeating Characters

PPZ發表於2015-02-20

http://www.cnblogs.com/zuoyuan/p/3785840.html
注意找到了重複的以後要把之前的全部置-1

class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
    start = 0
    maxlen = 0
    dict = {}
    for i in range(len(s)):
        dict[s[i]] = -1
    for i in range(len(s)):
        if dict[s[i]] != -1:
            while start <= dict[s[i]]:
                dict[s[start]] = -1
                start += 1
        if i - start + 1 > maxlen: maxlen = i - start + 1
        dict[s[i]] = i
    return maxlen

相關文章