Created
October 9, 2017 21:58
-
-
Save cixuuz/e017e4051cc2ebf022bb5190df27f61a to your computer and use it in GitHub Desktop.
[566. Reshape the Matrix] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int[][] matrixReshape(int[][] nums, int r, int c) { | |
| int m = nums.length; | |
| int n = nums[0].length; | |
| if (m * n != r * c) return nums; | |
| int[][] res = new int[r][c]; | |
| for (int i=0; i < r; i++) { | |
| for (int j=0; j<c; j++) { | |
| res[i][j] = nums[(i*c + j) / n][(i*c + j) % n]; | |
| } | |
| } | |
| return res; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment