Skip to content

Commit 8b6b96a

Browse files
committed
4 solved
1 parent aca532a commit 8b6b96a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

3sum/devyulbae.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Blind 75 - LeetCode Problem 15. 3Sum
3+
https://leetcode.com/problems/3sum/
4+
시간복잡도 O(n^2)
5+
"""
6+
from typing import List
7+
8+
class Solution:
9+
def threeSum(self, nums: List[int]) -> List[List[int]]:
10+
nums.sort()
11+
answer = []
12+
n = len(nums)
13+
14+
for i in range(n):
15+
if i > 0 and nums[i] == nums[i - 1]:
16+
continue # 중복 원소 스킵
17+
18+
left, right = i + 1, n - 1
19+
while left < right:
20+
total = nums[i] + nums[left] + nums[right]
21+
22+
if total == 0:
23+
answer.append([nums[i], nums[left], nums[right]])
24+
left += 1
25+
right -= 1
26+
27+
# 중복값 건너뛰기
28+
while left < right and nums[left] == nums[left - 1]:
29+
left += 1
30+
while left < right and nums[right] == nums[right + 1]:
31+
right -= 1
32+
33+
elif total < 0:
34+
left += 1 # 합이 작으니 left를 키워야 함
35+
36+
else:
37+
right -= 1 # 합이 크니 right를 줄여야 함
38+
39+
return answer
40+

0 commit comments

Comments
 (0)