scala version


  • 0
    M
      def matrixReshape(nums: Array[Array[Int]], r: Int, c: Int): Array[Array[Int]] = {
        var finalMatrix: Array[Array[Int]] = Array.ofDim(r, c)
        val hgt = nums.length
        val len = nums(0).length
        if (hgt * len != r * c) nums 
        else {
          for (i <- 0 until r * c) finalMatrix(i / c)(i % c) = nums(i / len)(i % len)
          finalMatrix
        }
      }
    

Log in to reply
 

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