Skip to content

Commit

Permalink
Create 1-Two-Sum.swift
Browse files Browse the repository at this point in the history
Swift version of Two Sum. Accepted by Leetcode Judge.
  • Loading branch information
chandra9302 authored Apr 4, 2022
1 parent 933ca4a commit 5a7f096
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions swift/1-Two-Sum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var prevMap = [Int:Int]() // val -> index

for (i, n) in nums.enumerated() {
let diff = target - n
if let firstIndex = prevMap[diff] {
return [firstIndex, i]
}
prevMap[n] = i
}
return []
}
}

0 comments on commit 5a7f096

Please sign in to comment.