FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc54.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc54.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (69 loc) · 1.85 KB
Breadcrumbs
leetcode
/
code
/
lc54.java
Copy path
File metadata and controls
70 lines (69 loc) · 1.85 KB
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
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
package
code
;
/*
* 54. Spiral Matrix
* 题意:螺旋输出矩阵
* 难度:Medium
* 分类:Array
* 思路:很直接的思路,就是循环打印
* Tips:需要注意很多细节,最后调试成功
*/
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
lc54
{
public
static
void
main
(
String
[]
args
) {
int
[][]
matrix
= {{
1
,
2
,
3
,
4
},{
5
,
6
,
7
,
8
},{
9
,
10
,
11
,
12
}};
System
.
out
.
println
(
spiralOrder
(
matrix
));
}
public
static
List
<
Integer
>
spiralOrder
(
int
[][]
matrix
) {
List
<
Integer
>
res
=
new
ArrayList
<>();
if
(
matrix
.
length
==
0
)
return
res
;
int
r
=
0
,
c
=-
1
;
// -1开始,循环内首先++
int
row_r
=
matrix
.
length
;
int
col_r
=
matrix
[
0
].
length
;
int
row_l
= -
1
;
int
col_l
=-
1
;
while
(
row_l
+
1
<
row_r
||
col_l
+
1
<
col_r
){
c
++;
if
(
c
<
col_r
){
while
(
c
<
col_r
){
res
.
add
(
matrix
[
r
][
c
]);
c
++;
}
}
else
//提前终止
return
res
;
c
--;
// -- 恢复正常
r
++;
if
(
r
<
row_r
) {
while
(
r
<
row_r
) {
res
.
add
(
matrix
[
r
][
c
]);
r
++;
}
}
else
return
res
;
r
--;
c
--;
if
(
c
>
col_l
){
while
(
c
>
col_l
){
res
.
add
(
matrix
[
r
][
c
]);
c
--;
}
}
else
return
res
;
c
++;
r
--;
if
(
r
>
row_l
+
1
) {
while
(
r
>
row_l
+
1
) {
res
.
add
(
matrix
[
r
][
c
]);
r
--;
}
}
else
return
res
;
r
++;
row_l
++;
row_r
--;
col_l
++;
col_r
--;
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL