Skip to content

Instantly share code, notes, and snippets.

@ksamirdev
Created April 13, 2026 08:10
Show Gist options
  • Select an option

  • Save ksamirdev/3983bd520a3951147c684f15e0c3381d to your computer and use it in GitHub Desktop.

Select an option

Save ksamirdev/3983bd520a3951147c684f15e0c3381d to your computer and use it in GitHub Desktop.
class Solution(object):
def threeSum(self, nums):
nums.sort()
res = []
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i-1]: # skip duplicate i
continue
low, high = i + 1, len(nums) - 1
while low < high:
s = nums[i] + nums[low] + nums[high]
if s == 0:
res.append([nums[i], nums[low], nums[high]])
while low < high and nums[low] == nums[low+1]:
low += 1
while low < high and nums[high] == nums[high-1]:
high -= 1
low += 1
high -= 1
elif s > 0:
high -= 1
else:
low += 1
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment