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

<p>Notice that the solution set must not contain duplicate triplets.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong> 
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 3000</code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        
        # ### Soln 1: lookup table which takes too long to check for duplicates
        # tab = {}
        # num_len = len(nums)
        # for i in range(num_len):
        #     cur_n = nums[i]
        #     if cur_n in tab:
        #         tab[cur_n].append(i)
        #     else:
        #         tab[cur_n] = [i]

        # out = []
        # for i in range(num_len - 2):
        #     for j in range(i+1, num_len - 1):
        #         diff = -(nums[i] + nums[j])
        #         if diff in tab:
        #             for temp_k in tab[diff]:
        #                 if temp_k > j and sorted([nums[i], nums[j], nums[temp_k]]) not in out:
        #                     out.append(sorted([nums[i], nums[j], nums[temp_k]]))
        # return out

        out = []
        pos = {}
        neg = {}
        zer = []
        for i in range(len(nums)):
            cur_num = nums[i]
            if cur_num == 0:
                zer.append(i)
            elif cur_num > 0:
                if cur_num in pos:
                    pos[cur_num].append(i)
                else:
                    pos[cur_num] = [i]
            elif cur_num < 0:
                if cur_num in neg:
                    neg[cur_num].append(i)
                else:
                    neg[cur_num] = [i]
        
        # Case 1: all zero
        if len(zer) >= 3:
            out.append([0,0,0])
        # Case 2: +, 0, -
        if len(zer) >= 1:
            for k in pos:
                if -k in neg:
                    out.append([k, 0, -k])
        # Case 3: +k, +k, -2k
        for k,v in pos.items():
            if len(v) >= 2:
                if -2*k in neg:
                    out.append([k, k, -2*k])
        # Case 4: -k, -k, +2k
        for k,v in neg.items():
            if len(v) >= 2:
                if -2*k in pos:
                    out.append([-2*k, k, k])
        # Case 5:
        pos_keys = list(pos.keys())
        for i in range(len(pos_keys)):
            for j in range(i + 1, len(pos_keys)):
                if -(pos_keys[i] + pos_keys[j]) in neg:
                    out.append([pos_keys[i], pos_keys[j], -(pos_keys[i] + pos_keys[j])])
        neg_keys = list(neg.keys())
        for i in range(len(neg_keys)):
            for j in range(i + 1, len(neg_keys)):
                if -(neg_keys[i] + neg_keys[j]) in pos:
                    out.append([-(neg_keys[i] + neg_keys[j]), neg_keys[i], neg_keys[j]])

        return out




        

                
21700