scala version


  • 0
    M
      def nextGreaterElement(findNums: Array[Int], nums: Array[Int]): Array[Int] = {
        var map = Map.empty[Int, Int]
        val stack = Stack[Int]()
        var arr = Array.empty[Int]
        for (num <- nums) {
          if (stack.nonEmpty && num > stack.top) {
            map += stack.top -> num
          }
          stack.push(num)
        }
    
        for (i <- findNums) {
          arr :+= map.getOrElse(i, -1)
        }
        arr
      }
    

Log in to reply
 

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