Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
995 B
1
Indexable
Never
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        n = len(nums)

        ans = []

        for i in range(n):
            if nums[i] > 0:
                break
            else:
                if i > 0 and nums[i] == nums[i-1]:
                    continue

            left = i + 1
            right = n - 1
            while left < right:
                summ = nums[i] + nums[left] + nums[right]
                print(summ)
                if summ > 0:
                    right -= 1
                elif summ < 0:
                    left += 1
                else:
                    ans.append([nums[i], nums[left], nums[right]])
                    left += 1
                    while nums[left] == nums[left-1] and left < right:
                        left += 1

                    while nums[right] == nums[right-1] and left < right:
                        right -= 1

        return ans
    
Leave a Comment