FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc542.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
/
lc542.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
38 lines (36 loc) · 1.22 KB
Breadcrumbs
leetcode
/
code
/
lc542.java
Copy path
File metadata and controls
38 lines (36 loc) · 1.22 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
package
code
;
/*
* 542. 01 Matrix
* 题意:0,1矩阵,每个点找离自己最近的0的距离
* 难度:Medium
* 分类:Depth-first Search, Breadth-first Search
* 思路:广度优先搜索
* Tips:
*/
import
java
.
util
.
LinkedList
;
import
java
.
util
.
Queue
;
public
class
lc542
{
public
int
[][]
updateMatrix
(
int
[][]
matrix
) {
Queue
<
int
[]>
qu
=
new
LinkedList
();
for
(
int
i
=
0
;
i
<
matrix
.
length
;
i
++){
for
(
int
j
=
0
;
j
<
matrix
[
0
].
length
;
j
++){
if
(
matrix
[
i
][
j
]==
0
){
qu
.
add
(
new
int
[]{
i
,
j
});
}
else
matrix
[
i
][
j
]=
Integer
.
MAX_VALUE
;
}
}
int
[][]
dirs
= {{-
1
,
0
},{
1
,
0
},{
0
,-
1
},{
0
,
1
}};
while
(!
qu
.
isEmpty
()){
int
[]
cell
=
qu
.
remove
();
for
(
int
i
=
0
;
i
<
4
;
i
++){
int
cur_row
=
cell
[
0
]+
dirs
[
i
][
0
];
int
cur_col
=
cell
[
1
]+
dirs
[
i
][
1
];
if
(
cur_row
<
0
||
cur_row
>=
matrix
.
length
||
cur_col
<
0
||
cur_col
>=
matrix
[
0
].
length
||
matrix
[
cur_row
][
cur_col
]<=
matrix
[
cell
[
0
]][
cell
[
1
]]+
1
)
continue
;
matrix
[
cur_row
][
cur_col
] =
matrix
[
cell
[
0
]][
cell
[
1
]]+
1
;
qu
.
add
(
new
int
[]{
cur_row
,
cur_col
});
}
}
return
matrix
;
}
}
Back
|
FazBrowse Home
|
New Git URL