Reshape the Matrix
给一个2d数组,和r和c, 返回重构后的int[r][c].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { if(r*c != nums.length * nums[0].length) return nums; int[][] res = new int[r][c]; int k = 0; int t = 0; for(int i = 0; i < nums.length; i++) { for(int j = 0; j < nums[0].length; j++) { res[k][t] = nums[i][j]; if(t < c - 1) { t++; } else { t = 0; k++; } } } return res; } } |