classSolution: defthreeSum(self, nums: List[int]) -> List[List[int]]: n=len(nums) res=[] if n<3ornot nums: return res nums.sort()
#从i遍历到n-2,使用双指针维护和探查; #记得去重,也就是对于相同的找最后的;
for i in range(0,n): if nums[i]>0: return res if i>0and nums[i]==nums[i-1]: continue#去重 L=i+1 R=n-1 while(L<R): if(nums[i]+nums[L]+nums[R]==0): res.append([nums[i],nums[L],nums[R]]) while(L<R and nums[L]==nums[L+1]): L=L+1 while L< R and nums[R]==nums[R-1]: R=R-1 L=L+1 R=R-1 elif nums[i]+nums[L]+nums[R]<0: L=L+1 else: R=R-1