forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixInSpiral.java
61 lines (56 loc) · 1.99 KB
/
MatrixInSpiral.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
59
60
61
package com.rampatra.arrays;
import static java.lang.System.out;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 9/9/15
* @time: 2:55 PM
*/
public class MatrixInSpiral {
/**
* Prints a 2D array {@param a} in spiral form (clockwise).
*
* @param a
*/
public static void printMatrixInSpiral(int[][] a) {
int row = a.length, col = a[0].length;
// this loop iterates for the entire matrix
for (int r = row, c = col, x = 0; r >= 0 && c >= 0; r--, c--, x++) {
/**
* Below 4 {@code for} loops print the perimeter of a matrix
*/
// prints the top row
for (int i = x, j = x; i < r && j < c; j++) {
out.print(a[i][j] + " ");
}
// prints the right most column
for (int i = x + 1, j = c - 1; i < r; i++) {
out.print(a[i][j] + " ");
}
// prints the bottom row
for (int i = r - 1, j = c - 2; i > x && j >= x; j--) {
out.print(a[i][j] + " ");
}
// prints the left most column
for (int i = r - 2, j = x; i > x; i--) {
out.print(a[i][j] + " ");
}
}
}
public static void main(String[] args) {
printMatrixInSpiral(new int[][]{{1}, {2}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2}, {3, 4}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2, 3}, {4, 5, 6}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}});
out.println();
printMatrixInSpiral(new int[][]{{1, 2, 3, 4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16}});
}
}