My Accepted Scala Solution


  • 0
    K

    def twoSum(nums: Array[Int], target: Int): Array[Int] = {
    var util:Map[Int,Int] = Map()
    var result = new ArrayBufferInt
    val loop = new Breaks
    loop.breakable{
    for (x <- 0 until nums.length){
    val left = target - nums(x)
    if(!util.contains(nums(x))){
    util += (left -> x)
    }else{

          result += util(nums(x))
          result += x
          
          loop.break()
        }
      }
    }
    result.toArray
    }

Log in to reply
 

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