原题链接:Reshape the Matrix
题解:
public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
/*
Time Complexity:O(M*N)
Space Complexity:O(M*N)
*/
int[][]res=new int[r][c];
int x=0,y=0;
if(nums.length==0 || r*c!=nums[0].length*nums.length)return nums;
for(int i=0;i<nums.length;i++)
{
for(int j=0;j<nums[0].length;j++){
res[x][y]=nums[i][j];
y++;
if(y==c){
x++;
y=0;
}
}
}
return res;
}
}