Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e64c51f

Browse files
added spiral order in java
1 parent 8e29b93 commit e64c51f

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.util.*;
2+
abstract class spiralOrder
3+
{
4+
/**
5+
* SAMPLE TEST CASE
6+
* 3 3
7+
* 1 2 3
8+
* 4 5 6
9+
* 7 8 9
10+
*
11+
* OUTPUT
12+
* 1
13+
* 2
14+
* 3
15+
* 6
16+
* 9
17+
* 8
18+
* 7
19+
* 4
20+
* 5
21+
**/
22+
public static void main(String[] args)
23+
{
24+
Scanner in = new Scanner(System.in);
25+
int n = in.nextInt();
26+
int m = in.nextInt();
27+
int[][] arr = new int[n][m];
28+
for(int i=0;i<n;i++)
29+
{
30+
for(int j=0;j<m;j++)
31+
{
32+
arr[i][j] = in.nextInt();
33+
}
34+
}
35+
printClock(arr,n,m);
36+
in.close();
37+
}
38+
39+
private static void printClock(int[][] arr,int n,int m)
40+
{
41+
int sr = 0;
42+
int er = n;
43+
int sc = 0;
44+
int ec = m;
45+
46+
while(sr < er && sc < ec)
47+
{
48+
// l -> r
49+
for(int i=sc;i<ec;i++)
50+
{
51+
System.out.println(arr[sr][i]);
52+
}
53+
sr++;
54+
55+
// |
56+
// v
57+
for(int i=sr;i<er;i++)
58+
{
59+
System.out.println(arr[i][ec-1]);
60+
}
61+
ec--;
62+
63+
//<-
64+
if(sr<er)
65+
{
66+
for(int i=ec-1;i>=sc;i--)
67+
{
68+
System.out.println(arr[er-1][i]);
69+
}
70+
er--;
71+
}
72+
73+
// ^
74+
// |
75+
if(sc<ec)
76+
{
77+
for(int i=er-1;i>=sr;i--)
78+
{
79+
System.out.println(arr[i][sc]);
80+
}
81+
sc++;
82+
}
83+
}
84+
}
85+
}

‎Java/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [ARRAYS CLASS](Data-Structures/ARRAYS/INBUILT/arrays.java)
1717
* MISC
1818
* JAGGED ARRAY
19+
* [SPIRAL ORDER MATRIX](Data-Structures/ARRAYS/MISC/spiralOrder.java)
1920

2021
#### STRING
2122

‎datastructures.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ Indexer for Data Structures Lover
8383
* [C](C/Data-Structures/ARRAYS/MISC/jaggedarray.c)
8484
* complexity
8585

86+
#### SPIRAL ORDER MATRIX
87+
88+
* blog/docs
89+
* implementation
90+
* [JAVA](Java/Data-Structures/ARRAYS/MISC/spiralOrder.java)
91+
* complexity
92+
8693
## :octocat: STRING
8794

8895
* blog

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /