Scala recursive solution


  • 0
    C
    object Solution {
        def twoSum(nums: Array[Int], target: Int): Array[Int] = {
            def getSum(hash: collection.mutable.HashMap[Int, Int], i: Int): Array[Int] = {
                if (hash contains nums(i)) Array(hash(nums(i)), i)
                else {hash += (target - nums(i) -> i); getSum(hash, i + 1)}
            }
            getSum(collection.mutable.HashMap[Int, Int](), 0)
        }
    }
    

Log in to reply
 

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