Created
March 27, 2019 08:45
-
-
Save muety/c3439d18121e5fff82231509bfaa3b95 to your computer and use it in GitHub Desktop.
LeetCode TwoSum Solution Scala
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
// https://leetcode.com/problems/two-sum | |
object TwoSum { | |
def apply(nums: Array[Int], target: Int): Array[Int] = { | |
nums.view.zipWithIndex.map(x => { | |
val el = nums.view.zipWithIndex.drop(x._2 + 1).collectFirst { | |
case y if y._1 + x._1 == target => y | |
} | |
Array(Option(x), el) | |
}).collectFirst { | |
case e if e.forall(_.isDefined) => e | |
}.head.map(_.get._2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment