Skip to content

Commit 63043b7

Browse files
feat: twoSum
1 parent 4c8b68f commit 63043b7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

two-sum/changchanghwang.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
func twoSum(nums []int, target int) []int {
4+
m := make(map[int]int)
5+
6+
// O(n)
7+
for i, num := range nums {
8+
// O(1)
9+
if j, ok := m[target-num]; ok && j != i { // target = num2 + num1 -> num2 = target - num1 을 이용하여 두 수를 찾는다.
10+
return []int{j, i}
11+
}
12+
m[num] = i // 없다면 현재 수를 키로 하여 인덱스를 저장한다.
13+
}
14+
return nil
15+
}

0 commit comments

Comments
 (0)