Skip to content

Commit 2d78341

Browse files
committed
Time: 3 ms (40.61%), Space: 20.3 MB (15.78%) - LeetHub
1 parent 9ac7262 commit 2d78341

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

0001-two-sum/0001-two-sum.swift

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

0 commit comments

Comments
 (0)