leetcode

Workout on LeetCode

View on GitHub

15. 3Sum

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Main ideas:

class Solution1:
    """
    """
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        
        res = []
        i = 0

        for i in range(len(nums)):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            
            j, k = i + 1, len(nums)-1

            while j < k:
                x = nums[i] + nums[j] + nums[k]
                if x > 0:
                    k -= 1
                elif x < 0:
                    j += 1
                else: # x == 0
                    res.append([nums[i], nums[j], nums[k]])
                    j += 1
                    while nums[j] == nums[j-1] and j < k: j += 1

        return res