Skip to content

Commit 7241279

Browse files
authored
Merge pull request neetcode-gh#26 from chandra9302/patch-3
Create 1-Two-Sum.swift
2 parents 1ccab17 + d3b4124 commit 7241279

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

swift/1-Two-Sum.swift

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/two-sum/
3+
*/
4+
5+
class TwoSum {
6+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
7+
var prevMap = [Int:Int]() // val -> index
8+
9+
for (i, n) in nums.enumerated() {
10+
let diff = target - n
11+
if let firstIndex = prevMap[diff] {
12+
return [firstIndex, i]
13+
}
14+
prevMap[n] = i
15+
}
16+
return []
17+
}
18+
}

0 commit comments

Comments
 (0)