Skip to content

Commit ff4aa6d

Browse files
author
MJ Kang
committed
3Sum #241
1 parent 915c79c commit ff4aa6d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

3sum/mandoolala.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
class Solution:
4+
def threeSum(self, nums: List[int]) -> List[List[int]]:
5+
answer = set()
6+
sorted_nums = sorted(nums)
7+
8+
for i in range(len(nums) - 2):
9+
low, high = i + 1, len(nums) - 1
10+
while low < high:
11+
three_sum = sorted_nums[i] + sorted_nums[low] + sorted_nums[high]
12+
if three_sum == 0:
13+
answer.add((sorted_nums[i], sorted_nums[low], sorted_nums[high]))
14+
low += 1
15+
high -= 1
16+
elif three_sum < 0:
17+
low += 1
18+
elif three_sum > 0:
19+
high -= 1
20+
return list(answer)
21+

0 commit comments

Comments
 (0)