share my java solution!


  • 0
    T
    public class Solution {
        public int[][] matrixReshape(int[][] nums, int r, int c) {
            int[][] res=new int[r][c];
            
            if(nums.length*nums[0].length!=r*c){
                return nums;
            }
            
            for(int i=0;i<nums.length;i++){
                for(int j=0;j<nums[0].length;j++){
                    int row=(i*nums[0].length+j)/c;
                    int col=(i*nums[0].length+j)%c;
                    res[row][col]=nums[i][j];
                }
            }
            
            return res;
        }
    }
    

Log in to reply
 

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