Skip to content

Instantly share code, notes, and snippets.

@popochess
Last active September 5, 2018 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popochess/ca41115087586c3cb5aa898222c88452 to your computer and use it in GitHub Desktop.
Save popochess/ca41115087586c3cb5aa898222c88452 to your computer and use it in GitHub Desktop.
[Leet Code] Two Sum
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