FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/Matrix/SpiralMatrix2.java at master · kothariji/competitive-programming · GitHub
kothariji
/
competitive-programming
Public
Notifications
You must be signed in to change notification settings
Fork
500
Star
704
Code
Issues
1
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
Matrix
/
SpiralMatrix2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (30 loc) · 954 Bytes
Breadcrumbs
competitive-programming
/
Matrix
/
SpiralMatrix2.java
Copy path
File metadata and controls
35 lines (30 loc) · 954 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class
Solution
{
public
int
[][]
generateMatrix
(
int
n
) {
int
matrix
[][] =
new
int
[
n
][
n
];
int
val
=
1
;
int
left
=
0
,
right
=
matrix
[
0
].
length
-
1
,
top
=
0
,
bottom
=
matrix
.
length
-
1
;
while
((
left
<=
right
) && (
top
<=
bottom
)) {
for
(
int
i
=
left
;
i
<=
right
;
i
++) {
matrix
[
top
][
i
] =
val
++;
}
top
++;
for
(
int
i
=
top
;
i
<=
bottom
;
i
++) {
matrix
[
i
][
right
] =
val
++;
}
right
--;
if
(
top
<=
bottom
) {
for
(
int
i
=
right
;
i
>=
left
;
i
--) {
matrix
[
bottom
][
i
] =
val
++;
}
}
bottom
--;
if
(
left
<=
right
) {
for
(
int
i
=
bottom
;
i
>=
top
;
i
--) {
matrix
[
i
][
left
] =
val
++;
}
left
++;
}
}
return
matrix
;
}
}
Back
|
FazBrowse Home
|
New Git URL