Skip to content

Commit 883e064

Browse files
committed
一刷1020
1 parent dae6bc1 commit 883e064

File tree

5 files changed

+119
-39
lines changed

5 files changed

+119
-39
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7165,14 +7165,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
71657165
//|{doc_base_url}/1019-next-greater-node-in-linked-list.adoc[题解]
71667166
//|Medium
71677167
//|
7168-
//
7169-
//|{counter:codes}
7170-
//|{leetcode_base_url}/number-of-enclaves/[1020. Number of Enclaves^]
7171-
//|{source_base_url}/_1020_NumberOfEnclaves.java[Java]
7172-
//|{doc_base_url}/1020-number-of-enclaves.adoc[题解]
7173-
//|Medium
7174-
//|
7175-
//
7168+
7169+
|{counter:codes}
7170+
|{leetcode_base_url}/number-of-enclaves/[1020. Number of Enclaves^]
7171+
|{source_base_url}/_1020_NumberOfEnclaves.java[Java]
7172+
|{doc_base_url}/1020-number-of-enclaves.adoc[题解]
7173+
|Medium
7174+
|
7175+
71767176
//|{counter:codes}
71777177
//|{leetcode_base_url}/remove-outermost-parentheses/[1021. Remove Outermost Parentheses^]
71787178
//|{source_base_url}/_1021_RemoveOutermostParentheses.java[Java]

docs/1020-number-of-enclaves.adoc

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,69 @@
11
[#1020-number-of-enclaves]
2-
= 1020. Number of Enclaves
2+
= 1020. 飞地的数量
33

4-
{leetcode}/problems/number-of-enclaves/[LeetCode - Number of Enclaves^]
4+
https://leetcode.cn/problems/number-of-enclaves/[LeetCode - 1020. 飞地的数量 ^]
55

6-
Given a 2D array `A`, each cell is 0 (representing sea) or 1 (representing land)
6+
给你一个大小为 `m x n` 的二进制矩阵 `grid` ,其中 `0` 表示一个海洋单元格、`1` 表示一个陆地单元格。
77

8-
A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.
8+
一次 *移动* 是指从一个陆地单元格走到另一个相邻(*上、下、左、右*)的陆地单元格或跨过 `grid` 的边界。
99

10-
Return the number of land squares in the grid for which we *cannot* walk off the boundary of the grid in any number of moves.
10+
返回网格中 *无法* 在任意次数的移动中离开网格边界的陆地单元格的数量。
1111

12-
13-
*Example 1:*
12+
*示例 1:*
1413

1514
image::images/1020-01.jpg[{image_attr}]
1615

17-
[subs="verbatim,quotes,macros"]
18-
----
19-
*Input:* [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
20-
*Output:* 3
21-
*Explanation:*
22-
There are three 1s that are enclosed by 0s, and one 1 that isn't enclosed because its on the boundary.
23-
----
16+
....
17+
输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
18+
输出:3
19+
解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。
20+
....
2421

25-
*Example 2:*
22+
*示例 2:*
2623

2724
image::images/1020-02.jpg[{image_attr}]
2825

29-
[subs="verbatim,quotes,macros"]
30-
----
31-
*Input:* [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
32-
*Output:* 0
33-
*Explanation:*
34-
All 1s are either on the boundary or can reach the boundary.
35-
36-
----
26+
....
27+
输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
28+
输出:0
29+
解释:所有 1 都在边界上或可以到达边界。
30+
....
3731

32+
*提示:*
3833

39-
*Note:*
34+
* `m == grid.length`
35+
* `n == grid[i].length`
36+
* `+1 <= m, n <= 500+`
37+
* `grid[i][j]` 的值为 `0``1`
4038
41-
. `1 <= A.length <= 500`
42-
. `1 <= A[i].length <= 500`
43-
. `0 <= A[i][j] <= 1`
44-
. All rows have the same size.
4539
4640
== 思路分析
4741

42+
从四条边出发,深度优先遍历,把能出逃的单元格都标记成负数,然后再遍历单元格,统计陆地数量即可。
43+
4844
[[src-1020]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
4950
[{java_src_attr}]
5051
----
5152
include::{sourcedir}/_1020_NumberOfEnclaves.java[tag=answer]
5253
----
54+
--
55+
56+
// 二刷::
57+
// +
58+
// --
59+
// [{java_src_attr}]
60+
// ----
61+
// include::{sourcedir}/_1020_NumberOfEnclaves_2.java[tag=answer]
62+
// ----
63+
// --
64+
====
65+
66+
67+
== 参考资料
68+
5369

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ include::1011-capacity-to-ship-packages-within-d-days.adoc[leveloffset=+1]
21232123

21242124
// include::1019-next-greater-node-in-linked-list.adoc[leveloffset=+1]
21252125

2126-
// include::1020-number-of-enclaves.adoc[leveloffset=+1]
2126+
include::1020-number-of-enclaves.adoc[leveloffset=+1]
21272127

21282128
// include::1021-remove-outermost-parentheses.adoc[leveloffset=+1]
21292129

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,11 @@ endif::[]
823823
|{doc_base_url}/1033-moving-stones-until-consecutive.adoc[题解]
824824
|✅ 分类讨论
825825

826+
|{counter:codes2503}
827+
|{leetcode_base_url}/number-of-enclaves/[1020. 飞地的数量^]
828+
|{doc_base_url}/1020-number-of-enclaves.adoc[题解]
829+
|✅ 深度优先遍历。有机会尝试一下广度优先遍历。
830+
826831

827832
|===
828833

src/main/java/com/diguage/algo/leetcode/_1020_NumberOfEnclaves.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,69 @@ public class _1020_NumberOfEnclaves {
44
// tag::answer[]
55
/**
66
* @author D瓜哥 · https://www.diguage.com
7-
* @since 2024-07-12 14:37:26
7+
* @since 2025-05-25 22:52:15
88
*/
99
public int numEnclaves(int[][] grid) {
10-
return 0;
10+
int row = grid.length;
11+
int col = grid[0].length;
12+
for (int i = 0; i < row; i++) {
13+
if (grid[i][0] == 1) {
14+
dfs(grid, i, 0);
15+
}
16+
if (grid[i][col - 1] == 1) {
17+
dfs(grid, i, col - 1);
18+
}
19+
}
20+
for (int i = 0; i < col; i++) {
21+
if (grid[0][i] == 1) {
22+
dfs(grid, 0, i);
23+
}
24+
if (grid[row - 1][i] == 1) {
25+
dfs(grid, row - 1, i);
26+
}
27+
}
28+
int result = 0;
29+
for (int i = 1; i < row - 1; i++) {
30+
for (int j = 1; j < grid[i].length - 1; j++) {
31+
if (grid[i][j] == 1) {
32+
result++;
33+
}
34+
}
35+
}
36+
return result;
37+
}
38+
39+
private void dfs(int[][] grid, int row, int column) {
40+
if (row < 0 || row >= grid.length
41+
|| column < 0 || column >= grid[row].length) {
42+
return;
43+
}
44+
if (grid[row][column] != 1) {
45+
return;
46+
}
47+
grid[row][column] = -1;
48+
// 上
49+
dfs(grid, row - 1, column);
50+
// 下
51+
dfs(grid, row + 1, column);
52+
// 左
53+
dfs(grid, row, column - 1);
54+
// 右
55+
dfs(grid, row, column + 1);
1156
}
1257
// end::answer[]
58+
public static void main(String[] args) {
59+
new _1020_NumberOfEnclaves().numEnclaves(new int[][]{
60+
{0, 0, 0, 1, 1, 1, 0, 1, 0, 0},
61+
{1, 1, 0, 0, 0, 1, 0, 1, 1, 1},
62+
{0, 0, 0, 1, 1, 1, 0, 1, 0, 0},
63+
{0, 1, 1, 0, 0, 0, 1, 0, 1, 0},
64+
{0, 1, 1, 1, 1, 1, 0, 0, 1, 0},
65+
{0, 0, 1, 0, 1, 1, 1, 1, 0, 1},
66+
{0, 1, 1, 0, 0, 0, 1, 1, 1, 1},
67+
{0, 0, 1, 0, 0, 1, 0, 1, 0, 1},
68+
{1, 0, 1, 0, 1, 1, 0, 0, 0, 0},
69+
{0, 0, 0, 0, 1, 1, 0, 0, 0, 1}
70+
});
71+
}
1372
}

0 commit comments

Comments
 (0)