Skip to content

Commit ba5559a

Browse files
committed
adding 3sum
1 parent 86c5f80 commit ba5559a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

3sum/daiyongg-kim.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def threeSum(self, nums: List[int]) -> List[List[int]]:
3+
nums.sort()
4+
5+
result = []
6+
n = len(nums)
7+
8+
for i in range(n - 2):
9+
10+
if i > 0 and nums[i] == nums[i - 1]:
11+
continue
12+
13+
left = i + 1
14+
right = n - 1
15+
while left < right:
16+
current_sum = nums[i] + nums[left] + nums[right]
17+
18+
if current_sum == 0:
19+
result.append([nums[i], nums[left], nums[right]])
20+
21+
while left < right and nums[left] == nums[left + 1]:
22+
left += 1
23+
24+
while left < right and nums[right] == nums[right - 1]:
25+
right -= 1
26+
27+
left += 1
28+
right -= 1
29+
30+
elif current_sum < 0:
31+
left += 1
32+
33+
else:
34+
right -= 1
35+
36+
return result

0 commit comments

Comments
 (0)