Python – Quadratic solution to 3Sum

I tried to solve a 3Sum problem in LeetCode:

Given a matrix numbers of $ n $ whole, there are elements $ a $, $ b $, $ c $ in numbers such that $ a + b + c = 0 $? Find all the unique triplets in the matrix that gives the sum of zero.

Note:

The set of solutions must not contain duplicate triplets.

Example:

Array numbers given = [-1, 0, 1, 2, -1, -4],

A set of solutions is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

I take advantage of the entire day to get such a solution.

Class solution: #Single pass approach

def threeSum (self, nums, target = 0) -> List[List[int]]:
""
: type numbers: List[int]
        : type target: int
: Rtype Triplets: List[List[int]]""
nums.sort ()
triplets = []

        def twoSum (numbers, destination, lo, hi) -> List[List[int]]:
""
: type numbers: List[int]
            : type target: int
""
search = {}

yes hello - what < 2: return []

            for lo in range(lo, hi):
                if lo > 1 and numbers[lo] == numbers[lo-2]: continue #kip the duplicates
yes nums[lo] > goal: break

num2 = nums[lo]
                complement = objective - num2
logging.debug (f " n  nsub_nums: {nums[lo:hi]} ")
logging.debug (f "num2: {num2}, is complementary: {complement}")

If lookup.get (add-on)! = None:
num3 = complement
Triplets.[num1, num2, num3])

search for[numbers[nums[números[nums[lo]]= what
logging.debug (f "lookup: {lookup}")
logging.debug (f "triplets: {triplets}")


yes len (nums) < 3: return [] 

        lo = 0
        hi = len(nums)
        for lo in range(lo, hi):
            if lo > 2 and numbers[lo] == numbers[lo-3]: continue #kip the duplicates
yes nums[lo] > goal: break

num1 = nums[lo]        

            twoSum (numbers, target-num1, lo + 1, hi)

spin triplets

Unfortunately, you get the wrong answer.

As to twoSum Solution to find all possible unique pairs.

Class solution: #Single pass approach
def twoSum (self, nums, target) -> List[List[int]]:
""
: type numbers: List[int]
        : type target: int
""
nums.sort ()
nums_d: dict = {}
couples = []

        yes len (nums) < 2:
            return []

        for i in range(len(nums)):
            if i > 1 and numbers[i] == numbers[i-2]: continue #kip the duplicates

complement = goal - numbers[i]
            logging.debug (f "add-on: {add-on}")

If nums_d.get (add-on)! = None:
Couples[numbers[nums[números[nums[i], complement])
nums_d[numbers[nums[números[nums[i]]= i
logging.debug (f "nums_d: {nums_d}")
logging.debug (f "couples: {couples}")
return couples 

It is done correctly

objective: 9
numbers [4, 7, 6, 3, 5]
DEBUG complement: 6
DEBUG nums_d: {3: 0}
DEBUG COUPLE []
DEBUG Add-on: 5
DEBUG nums_d: {3: 0, 4: 1}
DEBUG COUPLE []
DEBUG complement: 4
DEBUG nums_d: {3: 0, 4: 1, 5: 2}
DEBUG COUPLE [[5, 4]]DEBUG Add-on: 3
DEBUG nums_d: {3: 0, 4: 1, 5: 2, 6: 3}
DEBUG COUPLE [[5, 4], [6, 3]]DEBUG Add-on: 2
DEBUG nums_d: {3: 0, 4: 1, 5: 2, 6: 3, 7: 4}
DEBUG COUPLE [[5, 4], [6, 3]]result: [[5, 4], [6, 3]].
objective: 2
numbers [0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1]
DEBUG Add-on: 2
DEBUG nums_d: {0: 0}
DEBUG COUPLE []
DEBUG Add-on: 2
DEBUG nums_d: {0: 1}
DEBUG COUPLE []
DEBUG Add-on: 1
DEBUG nums_d: {0: 1, 1: 4}
DEBUG COUPLE []
DEBUG Add-on: 1
DEBUG nums_d: {0: 1, 1: 5}
DEBUG COUPLE [[1, 1]]result: [[1, 1]].
-------------------------------------------------- --------------------
Ran 2 tests at 0.001s

Could you please help some advice for my threeSum solution?