Recursive Scala Solution accepted


  • 0
    S
    object Solution {
        def twoSum(nums: Array[Int], target: Int): Array[Int] = {
            
            def getIndices(map:Map[Int, Int], index: Int): Array[Int] = {
                val current = nums(index)
    
                map.get(target - current) match {
                    case None => getIndices(map + (current -> index), index + 1)
                    case Some(otherIndex) => Array(otherIndex, index)
                }
            }
            getIndices(Map(), 0)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.