leetcode 3 无重复字符的最长子串
札:python中,圆括号表示元组,方括号表示列表,尖括号表示字典
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
st = {} #字典
i, ans = 0, 0
for j in range(len(s)): #遍历字符串
# 类似于map.containsKey(key)
if s[j] in st:
i = max(st[s[j]], i)
ans = max(ans, j-i+1)
#
st[s[j]] = j+1
return ans
用一个字典存以遍历元素及其位置,根据位置进行计算