-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReshape.java
More file actions
60 lines (49 loc) · 1.19 KB
/
Reshape.java
File metadata and controls
60 lines (49 loc) · 1.19 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Array;
/**
* Author - archit.s
* Date - 24/08/18
* Time - 12:50 PM
*/
public class Reshape {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int[][] result = new int[r][c];
int rows = nums.length, col = nums[0].length;
if(r*c != rows*col){
return nums;
}
int i1 = 0, j1 = 0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
result[i][j] = nums[i1][j1];
j1++;
if(j1 >= col){
j1= 0;
i1++;
}
}
}
return result;
}
// Alternate solution using Division & Modulus
// public int[][] matrixReshape(int[][] nums, int r, int c) {
//
// int[][] result = new int[r][c];
//
// int rows = nums.length, col = nums[0].length;
//
// if(r*c != rows*col){
// return nums;
// }
//
// int count = 0;
// for (int i = 0; i < nums.length; i++) {
// for (int j = 0; j < nums[0].length; j++) {
// result[count / c][count % c] = nums[i][j];
// count++;
// }
// }
//
// return result;
//
// }
}