-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReshapeMatrix.java
38 lines (35 loc) · 1.04 KB
/
ReshapeMatrix.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
package com.leetcode.problems;
public class ReshapeMatrix {
static int[][] matrixReshape(int[][] mat, int r, int c) {
if(r<=0 || c<=0 || mat.length*mat[0].length != r*c){
return(mat);
}
int [][] newMat = new int[r][c];
int rowCounter = 0;
int colCounter = 0;
for (int[] ints : mat) {
for (int j = 0; j < mat[0].length; j++) {
newMat[rowCounter][colCounter] = ints[j];
colCounter += 1;
if (colCounter == c) {
colCounter = 0;
rowCounter++;
}
}
}
return(newMat);
}
public static void main(String[] args) {
int[][] mat = {{1, 2}, {3, 4}, {5,6}};
int r = 2;
int c = 3;
int [][] result;
result = matrixReshape(mat, r, c);
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
System.out.print(" " + result[i][j]);
}
System.out.println();
}
}
}