public int[][] matrixReshape(int[][] nums, int r, int c) {
int n = nums.length, m = nums[0].length;
if (r*c != n*m) return nums;
int[][] res = new int[r][c];
for (int i=0;i<r*c;i++)
res[i/c][i%c] = nums[i/m][i%m];
return res;
}
Java Concise O(nm) time


public int[][] matrixReshape(int[][] nums, int r, int c) {
int newNums[] = null;
int mp[][]=null;
int len=0;
int index=0;
for(int i=0;i<nums.length;i++){
len+=nums[i].length;
}
newNums=new int [len];
for(int i=0;i<nums.length;i++){
for(int j=0;j<nums[i].length;j++){
newNums[index++]=nums[i][j];
}
}
int z=0;
int m=rc;
int firstLength=nums.length;
int secondLength=nums[0].length;
if(firstLengthsecondLength==m){
mp=new int[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
mp[i][j]=newNums[z++];
}
}return mp; } return nums;
}
//my runtime beats 43.55 % of java submissions