Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.41 KB | None | 0 0
  1.   // linear time and linear space
  2.   def twoSum(list: Array[Int], k: Int): Option[Result] = {
  3.     val cache = mutable.Map.empty[Int, Int]
  4.     for (i <- list.indices) {
  5.       val elem = list(i)
  6.       val dif = k - elem
  7.       val cacheValue = cache.get(dif)
  8.       if (cacheValue.isDefined) {
  9.         return Some(Result(cacheValue.get, i))
  10.       } else {
  11.         cache.put(elem, i)
  12.       }
  13.     }
  14.     None
  15.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement