Skip to content

Commit 8a48401

Browse files
author
sejineer
committed
3sum solution
1 parent 12e1701 commit 8a48401

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

3sum/sejineer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
시간 복잡도: O(N^2)
3+
공간 복잡도: O(N)
4+
"""
5+
class Solution:
6+
def threeSum(self, nums: List[int]) -> List[List[int]]:
7+
nums.sort()
8+
9+
result = set()
10+
11+
for i in range(len(nums) - 2):
12+
start, end = i + 1, len(nums) - 1
13+
while start < end:
14+
temp = nums[i] + nums[start] + nums[end]
15+
if temp == 0:
16+
result.add((nums[i], nums[start], nums[end]))
17+
start += 1
18+
end -= 1
19+
elif temp < 0:
20+
start += 1
21+
else:
22+
end -= 1
23+
24+
return list(result)

0 commit comments

Comments
 (0)