Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created April 8, 2019 19:48
Show Gist options
  • Save wszdwp/01a607b023c84d5ccc3b14f8911c065e to your computer and use it in GitHub Desktop.
Save wszdwp/01a607b023c84d5ccc3b14f8911c065e to your computer and use it in GitHub Desktop.
two sum
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dict = [Int: Int]()
for i in 0 ..< nums.count {
if (dict[target - nums[i]] != nil) {
return [dict[target - nums[i]]!, i]
}
dict[nums[i]] = i
}
return [Int]()
}
let nums = [2,7,11,15]
let target = 9
twoSum(nums, target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment