SHARE
TWEET

Untitled

a guest Sep 16th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
  16.  
  17. case class Result (
  18.     aIdx: Int,
  19.     bIdx: Int
  20. )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top