My java solution beats 99.63%


  • 0
    G
    public int[][] matrixReshape(int[][] nums, int r, int c) {
            int row = nums.length;
            int col = nums[0].length;
            if(row*col<r*c){
                return nums;
            }
            int[][] res = new int[r][c];
            int m=0 ,n=0;
            for(int i=0;i<r;i++){
                for(int j=0;j<c;j++){
                    if(n>=col){
                        n=0;
                        m++;
                    }
                    res[i][j]=nums[m][n++];
                }
            }
            return res;
        }
    

Log in to reply
 

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