-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspiralOrder.java
85 lines (80 loc) · 1.61 KB
/
spiralOrder.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
import java.util.*;
abstract class spiralOrder
{
/**
* SAMPLE TEST CASE
* 3 3
* 1 2 3
* 4 5 6
* 7 8 9
*
* OUTPUT
* 1
* 2
* 3
* 6
* 9
* 8
* 7
* 4
* 5
**/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] arr = new int[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
arr[i][j] = in.nextInt();
}
}
printClock(arr,n,m);
in.close();
}
private static void printClock(int[][] arr,int n,int m)
{
int sr = 0;
int er = n;
int sc = 0;
int ec = m;
while(sr < er && sc < ec)
{
// l -> r
for(int i=sc;i<ec;i++)
{
System.out.println(arr[sr][i]);
}
sr++;
// |
// v
for(int i=sr;i<er;i++)
{
System.out.println(arr[i][ec-1]);
}
ec--;
//<-
if(sr<er)
{
for(int i=ec-1;i>=sc;i--)
{
System.out.println(arr[er-1][i]);
}
er--;
}
// ^
// |
if(sc<ec)
{
for(int i=er-1;i>=sr;i--)
{
System.out.println(arr[i][sc]);
}
sc++;
}
}
}
}