The basic idea is:

- put all the elements in a one dimension array
- reshape the array to new matrix

```
public int[][] matrixReshape(int[][] nums, int r, int c) {
int l = nums.length;
int w = nums[0].length;
if(l * w != r* c)
return nums;
int[] combined = new int[r*c] ;
for(int i = 0; i< nums.length; i++)
System.arraycopy(nums[i], 0, combined, nums[i].length * i, nums[i].length);
int[][] ret = new int[r][c];
for (int i = 0; i < r; i++){
ret[i] = Arrays.copyOfRange(combined, c*i, c*i + c );
}
return ret;
}
```