题解 | #字符串的排列#
最长不含重复字符的子字符串
http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7
#coding:utf-8
代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
@param s string字符串
@return int整型
class Solution: def lengthOfLongestSubstring(self , s): # write code here ret = 0 curLen = 0 dist = dict()
for idx in range(len(s)):
lastDist = dist.get(s[idx], -1)
dist[s[idx]] = idx
if lastDist == -1:
curLen += 1
elif curLen >= (idx-lastDist):
curLen = idx-lastDist
elif curLen < idx-lastDist:
curLen += 1
print(idx, curLen, lastDist)
ret = max(ret, curLen)
return ret