3.Longest Substring Without Repeating Characters-python,3.longestrepeating
分享于 点击 22218 次 点评:125
3.Longest Substring Without Repeating Characters-python,3.longestrepeating
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.
Code
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
max=0
curmax=0
substr="" #当前的子字符串
i=0
length = len(s)
while i<length:
ind = substr.find(s[i])
if ind==-1:#no containing item
substr+=s[i]
curmax+=1
else:
if max < curmax:
max = curmax
i = i - len(substr) + ind #回溯
substr=""
curmax = 0
i+=1
if max < curmax:
max = curmax
return max
相关文章
- 暂无相关文章
用户点评