Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
问最长无重复字符的子串
想到记录这钱这个重复字符出现的位置
注意是字符,没说是小写字母,WA了一发
注意st和上次出现位置的前后顺序,要是上次出现位置在st之前就不用更新了,又WA了一发
吐槽py不能用字符直接减
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
pos=[]
for i in range(200):
pos.append(-1)
mlen=0
st=0
for i in range(len(s)):
if(pos[ord(s[i])]<>-1 and pos[ord(s[i])]>=st):
st=pos[ord(s[i])]+1
pos[ord(s[i])]=i
mlen=max(mlen,i-st+1)
return mlen