Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e4615a0

Browse files
author
Ram swaroop
committed
matrix rotation : done
1 parent c2ad1ea commit e4615a0

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package me.ramswaroop.arrays;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 8/22/15
8+
* @time: 4:03 PM
9+
*/
10+
public class RotateMatrixBy90Degrees {
11+
12+
public static int[][] rotateMatrixBy90DegreesRight(int[][] a) {
13+
int rows = a.length, columns = a[0].length;
14+
int[][] rotatedMatrix = new int[columns][rows];
15+
16+
for (int i = 0; --rows >= 0 && i < a.length; i++) {
17+
for (int j = 0; j < a[0].length; j++) {
18+
rotatedMatrix[j][rows] = a[i][j];
19+
}
20+
}
21+
22+
return rotatedMatrix;
23+
}
24+
25+
private static void print2DMatrix(int[][] a) {
26+
for (int i = 0; i < a.length; i++) {
27+
for (int j = 0; j < a[0].length; j++) {
28+
System.out.print(a[i][j]);
29+
}
30+
System.out.println();
31+
}
32+
}
33+
34+
public static void main(String a[]) {
35+
int[][] ar = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
36+
print2DMatrix(ar);
37+
System.out.println("--------");
38+
print2DMatrix(rotateMatrixBy90DegreesRight(ar));
39+
40+
System.out.println("========");
41+
42+
ar = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 5, 7}};
43+
print2DMatrix(ar);
44+
System.out.println("--------");
45+
print2DMatrix(rotateMatrixBy90DegreesRight(ar));
46+
}
47+
}

0 commit comments

Comments
 (0)