-
Notifications
You must be signed in to change notification settings - Fork 60
/
ReshapetheMatrix.java
29 lines (21 loc) · 765 Bytes
/
ReshapetheMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
Source: https://leetcode.com/problems/reshape-the-matrix/
Time: O(m * n), where m and n are the lengths of rows and columns respectively
Space: O(1), in-place, though we are using extra 2d array(res) but we will still treat space as O(1) and not O(n)
because we usually don't consider the output in the space complexity i.e. only temporary spaces which are used to get the desired output are considered.
*/
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int m = mat.length;
int n = mat[0].length;
int len = r * c;
if(m * n != len || m == r) {
return mat;
}
int[][] res = new int[r][c];
for(int i = 0; i < len; ++i) {
res[i / c][i % c] = mat[i / n][i % n];
}
return res;
}
}