public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int[][] res = new int[r][c];
if (nums == null || nums.length < 1 || nums[0].length < 1) return nums;
int m = nums.length;
int n = nums[0].length;
if (m * n != r * c) return nums;
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
queue.offer(nums[i][j]);
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
res[i][j] = queue.poll();
}
}
return res;
}
}
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length, n = nums[0].length;
if (r * c != m * n)
return nums;
int[][] reshaped = new int[r][c];
for (int i = 0; i < r * c; i++)
reshaped[i/c][i%c] = nums[i/n][i%n];
return reshaped;
}
public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int nRows = nums.length;
int nCols = nums[0].length;
if (nRows * nCols != r * c) {
return nums;
}
int[][] result = new int[r][c];
int currentRow = 0;
int currentCol = 0;
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nCols; j++) {
result[currentRow][currentCol] = nums[i][j];
currentCol++;
if (currentCol == c) {
currentCol = 0;
currentRow++;
}
}
}
return result;
}
}