Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def threeSum(self, nums: List[int]) -> List[List[int]]:
- nums.sort()
- triplets = []
- for i in range(len(nums)):
- # avoid duplicates to begin with
- if (i>0) and nums[i] == nums[i-1]:
- continue
- left = i + 1
- right = len(nums) - 1
- while (left < right):
- threeSum = nums[left] + nums[right] + nums[i]
- # duplicates will automatically be taken care of
- # in the next 2 conditions; You don't have to separately do that
- # Also, you have
- if (threeSum > 0):
- right -= 1
- elif (threeSum < 0):
- left += 1
- else:
- triplets.append([nums[i], nums[left], nums[right]])
- left += 1
- right -= 1
- # avoid duplicates by running the while loop checks
- while(left < right and nums[left] == nums[left - 1]):
- left += 1
- while(left < right and nums[right] == nums[right + 1]):
- right -= 1
- return triplets
Advertisement
Add Comment
Please, Sign In to add comment