From 4f10c4bb068543b8b3dd98c7321b9ff6450f62a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Sat, 12 Feb 2022 21:49:10 +0800 Subject: [PATCH] =?UTF-8?q?1020:=E9=A3=9E=E5=9C=B0=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/NumberOfEnclaves.java | 115 ++++++++++++++++++ .../editor/cn/doc/content/NumberOfEnclaves.md | 35 ++++++ 2 files changed, 150 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/NumberOfEnclaves.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/NumberOfEnclaves.md diff --git a/src/main/java/leetcode/editor/cn/NumberOfEnclaves.java b/src/main/java/leetcode/editor/cn/NumberOfEnclaves.java new file mode 100644 index 0000000..8763616 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NumberOfEnclaves.java @@ -0,0 +1,115 @@ +//给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。 +// +// 一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。 +// +// 返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。 +// +// +// +// 示例 1: +// +// +//输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] +//输出:3 +//解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。 +// +// +// 示例 2: +// +// +//输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] +//输出:0 +//解释:所有 1 都在边界上或可以到达边界。 +// +// +// +// +// 提示: +// +// +// m == grid.length +// n == grid[i].length +// 1 <= m, n <= 500 +// grid[i][j] 的值为 0 或 1 +// +// Related Topics 深度优先搜索 广度优先搜索 并查集 数组 矩阵 👍 103 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.TwoArray; + +import java.util.LinkedList; +import java.util.Queue; + +//1020:飞地的数量 +class NumberOfEnclaves { + public static void main(String[] args) { + //测试代码 + Solution solution = new NumberOfEnclaves().new Solution(); + TwoArray twoArray = new TwoArray("[[0],[1],[1],[0],[0]]", true); + solution.numEnclaves(twoArray.getArr()); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int numEnclaves(int[][] grid) { + boolean[][] use = new boolean[grid.length][grid[0].length]; + Queue queue = new LinkedList<>(); + int xl = grid.length; + int yl = grid[0].length; + int count = 0; + for (int i = 0; i < xl; i++) { + if (grid[i][0] == 1) { + queue.add(new int[]{i, 0}); + use[i][0] = true; + count++; + } + if (grid[i][yl - 1] == 1 && !use[i][yl - 1]) { + queue.add(new int[]{i, yl - 1}); + use[i][yl - 1] = true; + count++; + } + } + for (int i = 1; i < yl - 1; i++) { + if (grid[0][i] == 1 && !use[0][i]) { + queue.add(new int[]{0, i}); + use[0][i] = true; + count++; + } + if (grid[xl - 1][i] == 1 && !use[xl - 1][i]) { + queue.add(new int[]{xl - 1, i}); + use[xl - 1][i] = true; + count++; + } + } + int[] xp = new int[]{1, -1, 0, 0}; + int[] yp = new int[]{0, 0, 1, -1}; + while (!queue.isEmpty()) { + int[] arr = queue.poll(); + int x = arr[0]; + int y = arr[1]; + for (int k = 0; k < 4; k++) { + int nx = x + xp[k]; + int ny = y + yp[k]; + if (nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == 1 && !use[nx][ny]) { + queue.add(new int[]{nx, ny}); + use[nx][ny] = true; + count++; + } + } + } + int sum = 0; + for (int[] ints : grid) { + for (int j = 0; j < yl; j++) { + if (ints[j] == 1) { + sum++; + } + } + } + return sum - count; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/NumberOfEnclaves.md b/src/main/java/leetcode/editor/cn/doc/content/NumberOfEnclaves.md new file mode 100644 index 0000000..f485329 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/NumberOfEnclaves.md @@ -0,0 +1,35 @@ +

给你一个大小为 m x n 的二进制矩阵 grid ,其中 0 表示一个海洋单元格、1 表示一个陆地单元格。

+ +

一次 移动 是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过 grid 的边界。

+ +

返回网格中 无法 在任意次数的移动中离开网格边界的陆地单元格的数量。

+ +

 

+ +

示例 1:

+ +
+输入:grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
+输出:3
+解释:有三个 1 被 0 包围。一个 1 没有被包围,因为它在边界上。
+
+ +

示例 2:

+ +
+输入:grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
+输出:0
+解释:所有 1 都在边界上或可以到达边界。
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 深度优先搜索
  • 广度优先搜索
  • 并查集
  • 数组
  • 矩阵

  • 👍 103
  • 👎 0
  • \ No newline at end of file