-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiralMatrix.java
More file actions
44 lines (33 loc) · 785 Bytes
/
SpiralMatrix.java
File metadata and controls
44 lines (33 loc) · 785 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
34
35
36
37
38
39
40
41
42
43
44
package leetcode.practice;
public class SpiralMatrix {
public static void main(String[] args) {
int mat[][] = { {1 ,3 ,5 ,7 },
{10,11,16,20},
{23,30,34,50}};
//int mat[][] = {{1}};
spiralPrint(mat);
}
private static void spiralPrint(int[][] mat) {
int rows = mat.length; int cols = mat[0].length;
int row = 0; int col = cols - 1;
while (row < rows && col < cols) {
for(int i = 0 ; i < cols; i++) {
System.out.println(mat[row][i]);
}
row++;
for (int j = row; j < rows; j++ ) {
System.out.println(mat[j][col]);
row++;
}
col--;
for(int k=col; k > 0 ; k--) {
System.out.println(mat[row][k]);
col--;
}
row--;
for(int l=col; l > 0 ; k--) {
System.out.println(mat[row][k]);
}
}
}
}