MCPcopy Create free account
hub / github.com/Hsinha11/Leetcode-solutions / threeSum

Method threeSum

15-3sum/3sum.py:6–33  ·  view source on GitHub ↗
(self, nums: List[int])

Source from the content-addressed store, hash-verified

4
5class Solution:
6 def threeSum(self, nums: List[int]) -> List[List[int]]:
7 nums.sort()
8 result = []
9
10 for i in range(len(nums) - 2):
11 if i > 0 and nums[i] == nums[i - 1]:
12 continue
13
14 left = i + 1
15 right = len(nums) - 1
16
17 while left < right:
18 total = nums[i] + nums[left] + nums[right]
19
20 if total == 0:
21 result.append([nums[i], nums[left], nums[right]])
22 while left < right and nums[left] == nums[left + 1]:
23 left += 1
24 while left < right and nums[right] == nums[right - 1]:
25 right -= 1
26 left += 1
27 right -= 1
28 elif total < 0:
29 left += 1
30 else:
31 right -= 1
32
33 return result

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected