-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiral.java
More file actions
68 lines (57 loc) · 1.83 KB
/
Spiral.java
File metadata and controls
68 lines (57 loc) · 1.83 KB
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
62
63
64
65
66
67
//public class Spiral {
// public static void main(String[] args) {
// int[][] matrix = {
// {1, 2, 3},
// {4, 5, 6},
// {7, 8, 9}
// };
// int rowStart = 0, rowEnd = matrix.length - 1;
// int colStart = 0, colEnd = matrix[0].length - 1;
// System.out.println("Spiral order:");
// while (rowStart <= rowEnd && colStart <= colEnd)
// for (int col = colStart; col <= colEnd; col++) {
// System.out.print(matrix[rowStart][col] + " ");
// }
// rowStart++;
// for (int row = rowStart; row <= rowEnd; row++) {
// System.out.print(matrix[row][colEnd] + " ");
// }
// colEnd--;
// if (rowStart <= rowEnd) {
// for (int col = colEnd; col >= colStart; col--) {
// System.out.print(matrix[rowEnd][col] + " ");
// }
// rowEnd++
// ;
// }
// if (colStart <= colEnd) {
// for (int row = rowEnd; row >= rowStart; row--) {
// System.out.print(matrix[row][colStart] + " ");
// }
// colStart++;
// }
// }
// }
class Spiral2{
public void spiral(int[][] m) {
for(int i=0;i<3;i++){
System.out.println(m[0][i]);
}
for(int j=1;j<3;j++){
System.out.println(m[j][2]);
}
for(int i=1;i>=0;i--){
System.out.println(m[2][i]);
}
for(int j=0;j<2;j++){
System.out.println(m[1][j]);
}
}
}
public class Spiral {
public static void main(String[] args) {
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
Spiral2 x = new Spiral2();
x.spiral(a);
}
}