添加鏈接描述
class Solution:def lengthOfLongestSubstring(self, s: str) -> int:# 思路是使用隊列,加入一次取一個最大值,然后如果重復,則隊列出到沒有重復值位置# 但是這個隊列其實使用數組實現的strlist=[]temp=0result=0if len(s)==0:return resultfor i in s:if i not in strlist:strlist.append(i)temp+=1result=max(temp,result)else:while i in strlist:del strlist[0]temp-=1strlist.append(i)temp+=1result=max(temp,result)return result
我的思路:
- 隊列先進先出的特點
- 用數組實現隊列
Python刪除數組元素的幾個方法:
-
remove()
隊列名.remove(3):刪除數組中元素值為3的元素 -
del關鍵字
del 列表名[下標]
del 列表名[開始下標:結束下標]:不包括最后元素 -
clear()
列表名.clear():清空列表