Skip to content

Instantly share code, notes, and snippets.

@n1try n1try/TwoSum.scala
Created Mar 27, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.