Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def threeSum(self, nums: List[int]) -> List[List[int]]:
- newSums = []
- nums.sort()
- #This has to be minus 2 because your forward J Pointer will cause a tuple duplicate.
- for i in range(len(nums)- 2):
- #We need to skip duplicate elements from the left side as well. (This took me 15 minutes to realize).
- if i > 0 and nums[i] == nums[i-1]:
- continue
- #You have to reassign these accordingly during each loop. If you do it before this check you run into the same problem^.
- j = i + 1
- k = len(nums) - 1
- while j < k:
- if (nums[i] + nums[j] + nums[k]) == 0:
- newSums.append([nums[i], nums[j], nums[k]])
- #Avoiding another duplication problem i ran into.
- if k < len(nums) - 1 and nums[k] == nums[k + 1]:
- continue
- j+=1
- k-=1
- elif nums[i] + nums[j] + nums[k] > 0:
- k -= 1
- elif nums[i] + nums[j] + nums[k] < 0:
- j += 1
- return newSums
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement