Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 9, 2017 21:58
Show Gist options
  • Select an option

  • Save cixuuz/e017e4051cc2ebf022bb5190df27f61a to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/e017e4051cc2ebf022bb5190df27f61a to your computer and use it in GitHub Desktop.
[566. Reshape the Matrix] #leetcode
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