Simple Java Solution


  • 0
    S
    class Solution {
        public int[][] matrixReshape(int[][] nums, int r, int c) {
            int[][] result = new int[r][c];
            int n=nums.length;
            int m=nums[0].length;
            int y=c;
            if(n*m != r*c)
                return nums;
            --r;
            for(int i=n-1; i>=0; i--) {
                for(int j=m-1; j>=0; j--) {
                    y--;
                    if(y<0) {
                        r--;
                        y=c-1;
                    }
                    result[r][y] = nums[i][j];
                }
            }
            return result;
        }
    }

Log in to reply
 

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