My Ruby Solution


  • 0
    T
    def matrix_reshape(nums, r, c)
        nums_flatten = nums.flatten
        count = 0 
        result = Array.new(r) { Array.new(c) }
        
        return nums if r*c > nums_flatten.length
        
        (0...r).each do |row_idx|
            (0...c).each do |col_idx|
                result[row_idx][col_idx] = nums_flatten[count]
                count += 1
            end
        end
        
        result
    end
    

Log in to reply
 

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