Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // linear time and linear space
- def twoSum(list: Array[Int], k: Int): Option[Result] = {
- val cache = mutable.Map.empty[Int, Int]
- for (i <- list.indices) {
- val elem = list(i)
- val dif = k - elem
- val cacheValue = cache.get(dif)
- if (cacheValue.isDefined) {
- return Some(Result(cacheValue.get, i))
- } else {
- cache.put(elem, i)
- }
- }
- None
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement