Last active
January 10, 2019 01:46
-
-
Save jakebromberg/5f9012796aca5f451702 to your computer and use it in GitHub Desktop.
This file contains 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
extension Sequence where Element == Int { | |
func pairsOfSum(k: Int) -> [(Int, Int)] { | |
var cache = [Int:Int]() | |
var result = [(Int, Int)]() | |
for i in self { | |
cache[i] = k - i | |
} | |
for (num, inv) in cache { | |
if cache[inv] != nil { | |
result.append((num, inv)) | |
} | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment