-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathSpiralPrint.java
85 lines (71 loc) Β· 1.6 KB
/
SpiralPrint.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
input: 11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
output: 11 12 13 14 24 34 44 43 42 41 31 21 22 23 33 32
*/
package Lecture7;
public class SpiralPrint {
public static void main(String[] args){
int[][] arr = { { 11, 12, 13, 14 }, { 21, 22, 23, 24 },
{ 31, 32, 33, 34 }, { 41, 42, 43, 44 } };
display(arr);
System.out.println();
int top = 0, left = 0;
int right = arr[top].length - 1;
int bottom = arr.length - 1;
int direction = 1;
int count = (bottom + 1) * (right + 1);
while (left <= right && top <= bottom){
if (count > 0){
if (direction == 1){
for (int i = left; i <= right; i++){
System.out.print(arr[top][i] + " ");
count--;
}
top++;
direction = 2;
}
}
if (count > 0){
if (direction == 2){
for (int i = top; i <= bottom; i++){
System.out.print(arr[i][right] + " ");
count--;
}
direction = 3;
right--;
}
}
if (count > 0){
if (direction == 3){
for (int i = right; i >= left; i--){
System.out.print(arr[bottom][i] + " ");
count--;
}
direction = 4;
bottom--;
}
}
if (count > 0){
if (direction == 4){
for (int i = bottom; i >= top; i--){
System.out.print(arr[i][left] + " ");
count--;
}
direction = 1;
left++;
}
}
}
}
public static void display(int[][] arr){
for (int row = 0; row < arr.length; row++){
for (int col = 0; col < arr[row].length; col++){
System.out.print(arr[row][col]+ " ");
}
System.out.println();
}
}
}