-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
566.java
44 lines (40 loc) · 1.44 KB
/
566.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int or = nums.length;
int oc = nums[0].length;
if(or * oc != r * c) {
return nums;
}
int[][] newMatrix = new int[r][c];
for(int k = 0; k < r * c; k++) {
// Just test the index conversion here.
newMatrix[k / c][k % c] = nums[k / oc][k % oc];
}
return newMatrix;
}
}
__________________________________________________________________________________________________
sample 39084 kb submission
public class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int[][] res = new int[r][c];
if (nums.length == 0 || r * c != nums.length * nums[0].length)
return nums;
int count = 0;
Queue < Integer > queue = new LinkedList < > ();
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
queue.add(nums[i][j]);
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
res[i][j] = queue.remove();
}
}
return res;
}
}
__________________________________________________________________________________________________