Skip to content

Commit

Permalink
feat: 15. 3Sum - Time Limit
Browse files Browse the repository at this point in the history
  • Loading branch information
HodaeSsi committed Dec 21, 2024
1 parent a936fe7 commit 116e233
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 3sum/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 시간복잡도 O(n^2), 공간복잡도 O(n^2)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
# key: 값, value: list((i, j))
dic = {}
answer = set()

# 이중 for문으로 모든 경우의 수를 구합니다.
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] in dic:
dic[nums[i] + nums[j]].append((i, j))
else:
dic[nums[i] + nums[j]] = [(i, j)]

for k in range(len(nums)):
for i, j in dic.get(-nums[k], []):
if i != k and j != k:
answer.add(tuple(sorted([nums[i], nums[j], nums[k]])))

return list(answer)

0 comments on commit 116e233

Please sign in to comment.