Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Example
Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Solution
@简单的两重循环导致超时
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
used = {}
maxlenth,start = 0,0
for i,c in enumerate(s):
if c in used and start <= used[c]:
start = used[c]+1
else:
maxlenth = max(maxlenth,i-start+1)
used[c]=i
return maxlenth