One loop
We can use matrix[index / width][index % width] for both the input and the output matrix.\n\n    public int[][] matrixReshape(int[][] nums, int r, int c) {\n   
We can use `matrix[index / width][index % width]` for both the input and the output matrix.\n\n    public int[][] matrixReshape(int[][] nums, int r, int c) {\n        int m = nums.length, n = nums[0].length;\n        if (r * c != m * n)\n            return nums;\n        int[][] reshaped = new int[r][c];\n        for (int i = 0; i < r * c; i++)\n            reshaped[i/c][i%c] = nums[i/n][i%n];\n        return reshaped;\n    }
[]