Skip to content

Instantly share code, notes, and snippets.

@muety
Created March 27, 2019 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muety/c3439d18121e5fff82231509bfaa3b95 to your computer and use it in GitHub Desktop.
Save muety/c3439d18121e5fff82231509bfaa3b95 to your computer and use it in GitHub Desktop.
LeetCode TwoSum Solution Scala
// 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