Created
April 8, 2019 19:48
-
-
Save wszdwp/01a607b023c84d5ccc3b14f8911c065e to your computer and use it in GitHub Desktop.
two sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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