Skip to content

Commit 7c372f4

Browse files
committed
solve
1 parent 5dadc6e commit 7c372f4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

two-sum/sonjh1217.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
// time O(n) / space O(n)
3+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
4+
var indicesByCounters = [Int: Int]()
5+
for (i, num) in nums.enumerated() {
6+
if let index = indicesByCounters[num] {
7+
return [index, i]
8+
}
9+
10+
indicesByCounters[target - num] = i
11+
}
12+
13+
return []
14+
}
15+
}

0 commit comments

Comments
 (0)