Created
April 13, 2026 08:10
-
-
Save ksamirdev/3983bd520a3951147c684f15e0c3381d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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