最长不重复子串:
The firt coding;
Using list
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
arr = []
n = 0
for str in s :
if(str in arr) :
if(len(arr)>n) :
n = len(arr)
arr = arr[arr.index(str)+1:]
arr.append(str)
if(len(arr) > n):
n = len(arr)
return n
Runtime: 156 ms
The Second coding :
Using dict {}
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
arr = {}
n,i,j = 0,0,0
for str in s :
if(str in arr) :
n = max(j-i,n)
i = max(arr[str], i)
j+=1
arr[str] = j
n = max(j-i,n)
return n
Runtime: 116 ms