代码:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
length=len(nums)
nums.sort()
result=[]
if(not nums or length<3):
return result
elif(nums[0]>0):
return result
else:
for i in range(length-1):
L=i+1
R=length-1
while(L<R):
if(nums[i]+nums[L]+nums[R]==0):
if([nums[i],nums[L],nums[R]] in result):
result=result
else:
result.append([nums[i],nums[L],nums[R]])
if(nums[L]==nums[L+1]):
L+=1
if(nums[R]==nums[R-1]):
R-=1
L+=1
R-=1
elif(nums[i]+nums[L]+nums[R]<0):
L+=1
else:
借鉴的他人的思路,用字典和元组
day3
最新推荐文章于 2022-08-17 12:41:34 发布