添加1020.飞抵的数量Java版本的代码

This commit is contained in:
StriveDD
2023-03-13 21:26:29 +08:00
parent 549d37a54e
commit abc26c5e5a

View File

@ -144,6 +144,130 @@ public:
} }
}; };
``` ```
## 其他语言版本
**Java**
深度优先遍历版本:
```java
class Solution {
// 四个方向
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
// 深度优先遍历,把可以通向边缘部分的 1 全部标记成 true
public void dfs(int[][] grid, int row, int col, boolean[][] visited) {
for (int[] current: position) {
int newRow = row + current[0], newCol = col + current[1];
// 下标越界直接跳过
if (newRow < 0 || newRow >= grid.length || newCol < 0 || newCol >= grid[0].length) continue;
// 当前位置不是 1 或者已经被访问了就直接跳过
if (grid[newRow][newCol] != 1 || visited[newRow][newCol]) continue;
visited[newRow][newCol] = true;
dfs(grid, newRow, newCol, visited);
}
}
public int numEnclaves(int[][] grid) {
int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案
boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组
// 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历
for (int row = 0; row < rowSize; row++) {
if (grid[row][0] == 1 && !visited[row][0]) {
visited[row][0] = true;
dfs(grid, row, 0, visited);
}
if (grid[row][colSize - 1] == 1 && !visited[row][colSize - 1]) {
visited[row][colSize - 1] = true;
dfs(grid, row, colSize - 1, visited);
}
}
// 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了
for (int col = 1; col < colSize - 1; col++) {
if (grid[0][col] == 1 && !visited[0][col]) {
visited[0][col] = true;
dfs(grid, 0, col, visited);
}
if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) {
visited[rowSize - 1][col] = true;
dfs(grid, rowSize - 1, col, visited);
}
}
// 查找没有标记过的 1记录到 ans 中
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
if (grid[row][col] == 1 && !visited[row][col]) ++ans;
}
}
return ans;
}
}
```
广度优先遍历版本:
```java
class Solution {
// 四个方向
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
// 广度优先遍历,把可以通向边缘部分的 1 全部标记成 true
public void bfs(int[][] grid, Queue<int[]> queue, boolean[][] visited) {
while (!queue.isEmpty()) {
int[] curPos = queue.poll();
for (int[] current: position) {
int row = curPos[0] + current[0], col = curPos[1] + current[1];
// 下标越界直接跳过
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length)
continue;
// 当前位置不是 1 或者已经被访问了就直接跳过
if (visited[row][col] || grid[row][col] == 0) continue;
visited[row][col] = true;
queue.add(new int[]{row, col});
}
}
}
public int numEnclaves(int[][] grid) {
int rowSize = grid.length, colSize = grid[0].length, ans = 0; // ans 记录答案
boolean[][] visited = new boolean[rowSize][colSize]; // 标记数组
Queue<int[]> queue = new ArrayDeque<>();
// 左侧边界和右侧边界查找 1 进行标记并进行深度优先遍历
for (int row = 0; row < rowSize; row++) {
if (grid[row][0] == 1) {
visited[row][0] = true;
queue.add(new int[]{row, 0});
}
if (grid[row][colSize - 1] == 1) {
visited[row][colSize - 1] = true;
queue.add(new int[]{row, colSize - 1});
}
}
// 上边界和下边界遍历,但是四个角不用遍历,因为上面已经遍历到了
for (int col = 1; col < colSize - 1; col++) {
if (grid[0][col] == 1) {
visited[0][col] = true;
queue.add(new int[]{0, col});
}
if (grid[rowSize - 1][col] == 1 && !visited[rowSize - 1][col]) {
visited[rowSize - 1][col] = true;
queue.add(new int[]{rowSize - 1, col});
}
}
bfs(grid, queue, visited);
// 查找没有标记过的 1记录到 ans 中
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < colSize; col++) {
if (grid[row][col] == 1 && !visited[row][col]) ++ans;
}
}
return ans;
}
}
```
## 类似题目 ## 类似题目
* 1254. 统计封闭岛屿的数目 * 1254. 统计封闭岛屿的数目
@ -153,3 +277,4 @@ public:
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>