Skip to content

Commit e4c8c55

Browse files
committed
two-sum sol (py)
1 parent cca1872 commit e4c8c55

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

two-sum/hi-rachel.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
"""
2-
처음 풀이
3-
O(N^2) time, O(N) space
4-
"""
2+
https://leetcode.com/problems/two-sum/
53
6-
# class Solution:
7-
# def twoSum(self, nums: List[int], target: int) -> List[int]:
8-
# result = []
9-
# for i in range(len(nums)):
10-
# rest = target - nums[i]
11-
# rest_nums = nums[i+1:]
12-
# if rest in rest_nums:
13-
# result.extend([i, rest_nums.index(rest)+i+1])
14-
# break
15-
# return result
16-
4+
Given an array of integers nums and an integer target,
5+
return indices of the two numbers such that they add up to target.
6+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
You can return the answer in any order.
178
18-
"""
199
개선 코드
2010
O(N) time, O(N) space
2111
"""
2212

13+
from typing import List
14+
2315
class Solution:
2416
def twoSum(self, nums: List[int], target: int) -> List[int]:
2517
indices = {}
@@ -31,6 +23,24 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
3123
return [i, j]
3224
indices[v] = i
3325

26+
27+
"""
28+
처음 풀이
29+
O(N^2) time, O(N) space
30+
"""
31+
32+
# class Solution:
33+
# def twoSum(self, nums: List[int], target: int) -> List[int]:
34+
# result = []
35+
# for i in range(len(nums)):
36+
# rest = target - nums[i]
37+
# rest_nums = nums[i+1:]
38+
# if rest in rest_nums:
39+
# result.extend([i, rest_nums.index(rest)+i+1])
40+
# break
41+
# return result
42+
43+
3444
# JS 풀이
3545
# /**
3646
# * @param {number[]} nums

0 commit comments

Comments
 (0)