一、leetcode地址
https://leetcode.com/problems/3sum/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
print(f'nums:{nums}')
n = len(nums);l1 = []
for i in range(n):
if i > 0 and nums[i] == nums[i-1]:
continue
l,r = i+1,n-1
while l<r:
temp_sum = nums[i] + nums[l] + nums[r]
if temp_sum == 0:
l1.append([nums[i],nums[l],nums[r]])
while l<r and nums[l] == nums[l+1]:
l += 1
while l<r and nums[r] == nums[r-1]:
r -= 1
l+=1
r-=1
elif temp_sum > 0:
r -= 1
else:
l += 1
return l1