LeetCode 3. longest characters & 切片
Longest Substring Without Repeating Characters
找无重复的最长子串
第1次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
#print(s[i:k+1],s[i:k+1].count(c),k-i)
if s[i:k+1].count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
return maxLen
if __name__ == "__main__":
sl=[
'abcabcbb','bbbbb','pwwkew','','c','au'
];
for s in sl:
print(Solution().lengthOfLongestSubstring(s))
print("end-------------")
Wrong Answer:
Input:
"c"
Output:
0
Expected:
1
没有重复的时候忘了赋值了,for之后如果还未0就计算长度
第2次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
print(s[i:k+1],s[i:k+1].count(c),k-i)
if s[i:k+1].count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
if maxLen==0:
maxLen=len(s)
return maxLen
Wrong Answer:
Input:
"aab"
Output:
1
Expected:
2
想了一阵,发现我的逻辑有问题,for每次都应该计算长度,有重复则计算切片-1的,无重复则计算切片长度。看测试数据感觉没漏洞了,提交下:
第3次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
# 最后的切片
sp=[]
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
sp=s[i:k+1]
print(sp,sp.count(c),k-i)
# 这里分两种,1中有重复则计算之前的长度,无重复则计算现在长度
if sp.count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
else:
if len(sp)>maxLen:
maxLen = len(sp)
return maxLen
Wrong Answer:
Input:
"dvdf"
Output:
2
Expected:
3
再次推翻了之前的想法,不应该直接重复就再次计算的,应该重复了,从起点+1切片再次开始
第4次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
# 最后的字符
lastChar=None
# 在处理的切片
sp=[]
# 切片起点
i=0
# 当前下标
k=0
while k!=len(s[i:]):
c=s[i:][k]
sp=s[i:i+k+1]
#print(sp,c,sp.count(c),"sp:",len(sp),end=" len:")
# 这里分两种,1中有重复则计算之前的长度并且起点+1切片重新开始for。无重复则计算现在长度
if sp.count(c) > 1 :
findLen=len(sp)-1
# 重新计算的切片起点
i+=1
k=0
else:
findLen=len(sp)
k+=1
if findLen>maxLen:
maxLen=findLen
#print(maxLen)
return maxLen
Time Limit Exceeded:
Last executed input:
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~
......
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCD"
稍微优化一下切片的计算次数
第5次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
# 最后的字符
lastChar=None
# 在处理的切片
sp=[]
# 切片起点
i=0
# 当前下标
k=0
lists=s[i:]
lens=len(lists)
while k!=lens:
c=lists[k]
sp=lists[:k+1]
#print(sp,c,sp.count(c),"sp:",len(sp),end=" len:")
# 这里分两种,1中有重复则计算之前的长度并且起点+1切片重新开始for。无重复则计算现在长度
if sp.count(c) > 1 :
findLen=len(sp)-1
# 重新计算的切片起点
i+=1
lists=s[i:]
lens=len(lists)
k=0
else:
findLen=len(sp)
k+=1
if findLen>maxLen:
maxLen=findLen
#print(maxLen)
return maxLen
总结:思考不够全面。