We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4c8b68f commit 63043b7Copy full SHA for 63043b7
two-sum/changchanghwang.go
@@ -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