forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrixBy90Degrees.java
58 lines (50 loc) · 1.5 KB
/
RotateMatrixBy90Degrees.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.rampatra.arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 8/22/15
* @time: 4:03 PM
*/
public class RotateMatrixBy90Degrees {
/**
* Rotates a 2-D array by 90 degrees clockwise.
* <p/>
* The algorithm is simple:
* 1st row = last column
* 2nd row = 2nd last column
* and so on...
*
* @param a
* @return
*/
public static int[][] rotateMatrixBy90DegreesRight(int[][] a) {
int rows = a.length, columns = a[0].length;
int[][] rotatedMatrix = new int[columns][rows];
for (int i = 0; --rows >= 0 && i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
rotatedMatrix[j][rows] = a[i][j];
}
}
return rotatedMatrix;
}
private static void print2DMatrix(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
int[][] ar = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print2DMatrix(ar);
System.out.println("--------");
print2DMatrix(rotateMatrixBy90DegreesRight(ar));
System.out.println("========");
ar = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 5, 7}};
print2DMatrix(ar);
System.out.println("--------");
print2DMatrix(rotateMatrixBy90DegreesRight(ar));
}
}