- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_4.java
More file actions
Latest commit
33 lines (30 loc) · 986 Bytes
/
Copy pathDemo_4.java
File metadata and controls
33 lines (30 loc) · 986 Bytes
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
importjava.util.Arrays;
classTransposeMatrix{
// public int[][] transpose(int[][] matrix) {
// for(int i=0;i<matrix.length;i++){
// for(int j=1;j<matrix[i].length;j++){
// int temp = matrix[i][j];
// matrix[i][j] = matrix[j][i];
// matrix[j][i] = temp;
// }
// }
// return matrix;
// }
publicint[][] transpose(int[][] matrix) {
int[][] arr = newint[matrix[0].length][matrix.length];
for(inti=0;i<matrix.length;i++){
for(intj=0;j<matrix[i].length;j++){
arr[i][j] = matrix[j][i];
}
}
returnarr;
}
}
publicclassDemo_4 {
publicstaticvoidmain(String[] args) {
TransposeMatrixtm = newTransposeMatrix();
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
// Output: [[1,4,7],[2,5,8],[3,6,9]]
System.out.println(Arrays.deepToString(tm.transpose(matrix)));
}
}