---
description: LeetCode 15
---

# 3 Sum

```python
class Solution:
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums.sort()
        #[-4, -1, -1, 0, 1 ,2]
        res = []
        for i in range(len(nums)):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            a = nums[i]
            left = i + 1
            right = len(nums) - 1
            
            while left < right:
                b = nums[left]
                c = nums[right]
                target = a + b + c
                
                if target == 0:
                    res.append((a, b, c))
                    while left < right and nums[left] == nums[left+1]:
                        left = left + 1
                    while left < right and nums[right] == nums[right-1]:
                        right = right - 1 
                    left = left + 1
                    right = right - 1
                elif target < 0:
                    left = left + 1
                elif target > 0:
                    right = right -1       

        return res
            
```
