MCPcopy Create free account
hub / github.com/BeeBombshell/Python-DSA / threeSum

Function threeSum

DSA/three_sum.py:2–26  ·  view source on GitHub ↗
(self, nums: List[int])

Source from the content-addressed store, hash-verified

1# Three Sum dynamic problem solution
2def threeSum(self, nums: List[int]) -> List[List[int]]:
3 res = []
4 nums.sort()
5
6 for i, num1 in enumerate(nums):
7
8 if i > 0 and num1 == nums[i-1]:
9 continue
10
11 start, end = i+1, len(nums) - 1
12
13 while start < end:
14 sum = num1 + nums[start] + nums[end]
15
16 if sum > 0:
17 end -= 1
18 elif sum < 0:
19 start += 1
20 else:
21 res.append([num1, nums[start], nums[end]])
22 start += 1
23 while nums[start] == nums[start-1] and start < end:
24 start += 1
25
26 return res

Callers

nothing calls this directly

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected