Last active
September 5, 2018 14:32
-
-
Save popochess/ca41115087586c3cb5aa898222c88452 to your computer and use it in GitHub Desktop.
[Leet Code] 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 dic = [Int : Int]() | |
for (i, element) in nums.enumerated() { | |
let diff = target - element | |
if (dic.keys.contains(diff)) { | |
return [dic[diff]!,i] | |
} | |
dic[element] = i | |
} | |
return [0] | |
} | |
twoSum([2,1,9,4,4,56,90,3],8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment