SHARE
TWEET

Untitled

a guest Sep 16th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //constant space, n log n + n time
  2. def twoSumConstSpace(list: Array[Int], k: Int): Option[Result] = {
  3.   util.Arrays.sort(list)
  4.   var i = 0
  5.   var j = list.length - 1
  6.   while (i < j) {
  7.     val sum = list(i) + list(j)
  8.     if (sum == k) {
  9.       return Some(Result(i, j))
  10.     } else if (sum < k) {
  11.       i += 1
  12.     } else {
  13.       j -= 1
  14.     }
  15.   }
  16.   None
  17. }
  18.  
  19. case class Result (
  20.     aIdx: Int,
  21.     bIdx: Int
  22. )
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
 
Top