mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1931 from StriveDD/master
添加0130.被围绕的区域的Java版本的 BFS、DFS 代码,0797.所有可能的路径的Java版本的 DFS 代码
This commit is contained in:
@ -84,7 +84,221 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
|
||||
```Java
|
||||
// 广度优先遍历
|
||||
// 使用 visited 数组进行标记
|
||||
class Solution {
|
||||
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向
|
||||
|
||||
public void solve(char[][] board) {
|
||||
// rowSize:行的长度,colSize:列的长度
|
||||
int rowSize = board.length, colSize = board[0].length;
|
||||
boolean[][] visited = new boolean[rowSize][colSize];
|
||||
Queue<int[]> queue = new ArrayDeque<>();
|
||||
// 从左侧边,和右侧边遍历
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
if (board[row][0] == 'O') {
|
||||
visited[row][0] = true;
|
||||
queue.add(new int[]{row, 0});
|
||||
}
|
||||
if (board[row][colSize - 1] == 'O') {
|
||||
visited[row][colSize - 1] = true;
|
||||
queue.add(new int[]{row, colSize - 1});
|
||||
}
|
||||
}
|
||||
// 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角
|
||||
// 所以在遍历上边和下边时可以不用遍历四个角
|
||||
for (int col = 1; col < colSize - 1; col++) {
|
||||
if (board[0][col] == 'O') {
|
||||
visited[0][col] = true;
|
||||
queue.add(new int[]{0, col});
|
||||
}
|
||||
if (board[rowSize - 1][col] == 'O') {
|
||||
visited[rowSize - 1][col] = true;
|
||||
queue.add(new int[]{rowSize - 1, col});
|
||||
}
|
||||
}
|
||||
// 广度优先遍历,把没有被 'X' 包围的 'O' 进行标记
|
||||
while (!queue.isEmpty()) {
|
||||
int[] current = queue.poll();
|
||||
for (int[] pos: position) {
|
||||
int row = current[0] + pos[0], col = current[1] + pos[1];
|
||||
// 如果范围越界、位置已被访问过、该位置的值不是 'O',就直接跳过
|
||||
if (row < 0 || row >= rowSize || col < 0 || col >= colSize) continue;
|
||||
if (visited[row][col] || board[row][col] != 'O') continue;
|
||||
visited[row][col] = true;
|
||||
queue.add(new int[]{row, col});
|
||||
}
|
||||
}
|
||||
// 遍历数组,把没有被标记的 'O' 修改成 'X'
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
for (int col = 0; col < colSize; col++) {
|
||||
if (board[row][col] == 'O' && !visited[row][col]) board[row][col] = 'X';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
```Java
|
||||
// 广度优先遍历
|
||||
// 直接修改 board 的值为其他特殊值
|
||||
class Solution {
|
||||
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向
|
||||
|
||||
public void solve(char[][] board) {
|
||||
// rowSize:行的长度,colSize:列的长度
|
||||
int rowSize = board.length, colSize = board[0].length;
|
||||
Queue<int[]> queue = new ArrayDeque<>();
|
||||
// 从左侧边,和右侧边遍历
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
if (board[row][0] == 'O')
|
||||
queue.add(new int[]{row, 0});
|
||||
if (board[row][colSize - 1] == 'O')
|
||||
queue.add(new int[]{row, colSize - 1});
|
||||
}
|
||||
// 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角
|
||||
// 所以在遍历上边和下边时可以不用遍历四个角
|
||||
for (int col = 1; col < colSize - 1; col++) {
|
||||
if (board[0][col] == 'O')
|
||||
queue.add(new int[]{0, col});
|
||||
if (board[rowSize - 1][col] == 'O')
|
||||
queue.add(new int[]{rowSize - 1, col});
|
||||
}
|
||||
// 广度优先遍历,把没有被 'X' 包围的 'O' 修改成特殊值
|
||||
while (!queue.isEmpty()) {
|
||||
int[] current = queue.poll();
|
||||
board[current[0]][current[1]] = 'A';
|
||||
for (int[] pos: position) {
|
||||
int row = current[0] + pos[0], col = current[1] + pos[1];
|
||||
// 如果范围越界、该位置的值不是 'O',就直接跳过
|
||||
if (row < 0 || row >= rowSize || col < 0 || col >= colSize) continue;
|
||||
if (board[row][col] != 'O') continue;
|
||||
queue.add(new int[]{row, col});
|
||||
}
|
||||
}
|
||||
// 遍历数组,把 'O' 修改成 'X',特殊值修改成 'O'
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
for (int col = 0; col < colSize; col++) {
|
||||
if (board[row][col] == 'A') board[row][col] = 'O';
|
||||
else if (board[row][col] == 'O') board[row][col] = 'X';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
```Java
|
||||
// 深度优先遍历
|
||||
// 使用 visited 数组进行标记
|
||||
class Solution {
|
||||
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向
|
||||
|
||||
public void dfs(char[][] board, int row, int col, boolean[][] visited) {
|
||||
for (int[] pos: position) {
|
||||
int nextRow = row + pos[0], nextCol = col + pos[1];
|
||||
// 位置越界
|
||||
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length)
|
||||
continue;
|
||||
// 位置已被访问过、新位置值不是 'O'
|
||||
if (visited[nextRow][nextCol] || board[nextRow][nextCol] != 'O') continue;
|
||||
visited[nextRow][nextCol] = true;
|
||||
dfs(board, nextRow, nextCol, visited);
|
||||
}
|
||||
}
|
||||
|
||||
public void solve(char[][] board) {
|
||||
int rowSize = board.length, colSize = board[0].length;
|
||||
boolean[][] visited = new boolean[rowSize][colSize];
|
||||
// 从左侧遍、右侧遍遍历
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
if (board[row][0] == 'O' && !visited[row][0]) {
|
||||
visited[row][0] = true;
|
||||
dfs(board, row, 0, visited);
|
||||
}
|
||||
if (board[row][colSize - 1] == 'O' && !visited[row][colSize - 1]) {
|
||||
visited[row][colSize - 1] = true;
|
||||
dfs(board, row, colSize - 1, visited);
|
||||
}
|
||||
}
|
||||
// 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角
|
||||
// 所以在遍历上边和下边时可以不用遍历四个角
|
||||
for (int col = 1; col < colSize - 1; col++) {
|
||||
if (board[0][col] == 'O' && !visited[0][col]) {
|
||||
visited[0][col] = true;
|
||||
dfs(board, 0, col, visited);
|
||||
}
|
||||
if (board[rowSize - 1][col] == 'O' && !visited[rowSize - 1][col]) {
|
||||
visited[rowSize - 1][col] = true;
|
||||
dfs(board, rowSize - 1, col, visited);
|
||||
}
|
||||
}
|
||||
// 遍历数组,把没有被标记的 'O' 修改成 'X'
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
for (int col = 0; col < colSize; col++) {
|
||||
if (board[row][col] == 'O' && !visited[row][col]) board[row][col] = 'X';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
```Java
|
||||
// 深度优先遍历
|
||||
// // 直接修改 board 的值为其他特殊值
|
||||
class Solution {
|
||||
private static final int[][] position = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // 四个方向
|
||||
|
||||
public void dfs(char[][] board, int row, int col) {
|
||||
for (int[] pos: position) {
|
||||
int nextRow = row + pos[0], nextCol = col + pos[1];
|
||||
// 位置越界
|
||||
if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length)
|
||||
continue;
|
||||
// 新位置值不是 'O'
|
||||
if (board[nextRow][nextCol] != 'O') continue;
|
||||
board[nextRow][nextCol] = 'A'; // 修改为特殊值
|
||||
dfs(board, nextRow, nextCol);
|
||||
}
|
||||
}
|
||||
|
||||
public void solve(char[][] board) {
|
||||
int rowSize = board.length, colSize = board[0].length;
|
||||
// 从左侧遍、右侧遍遍历
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
if (board[row][0] == 'O') {
|
||||
board[row][0] = 'A';
|
||||
dfs(board, row, 0);
|
||||
}
|
||||
if (board[row][colSize - 1] == 'O') {
|
||||
board[row][colSize - 1] = 'A';
|
||||
dfs(board, row, colSize - 1);
|
||||
}
|
||||
}
|
||||
// 从上边和下边遍历,在对左侧边和右侧边遍历时我们已经遍历了矩阵的四个角
|
||||
// 所以在遍历上边和下边时可以不用遍历四个角
|
||||
for (int col = 1; col < colSize - 1; col++) {
|
||||
if (board[0][col] == 'O') {
|
||||
board[0][col] = 'A';
|
||||
dfs(board, 0, col);
|
||||
}
|
||||
if (board[rowSize - 1][col] == 'O') {
|
||||
board[rowSize - 1][col] = 'A';
|
||||
dfs(board, rowSize - 1, col);
|
||||
}
|
||||
}
|
||||
// 遍历数组,把 'O' 修改成 'X',特殊值修改成 'O'
|
||||
for (int row = 0; row < rowSize; row++) {
|
||||
for (int col = 0; col < colSize; col++) {
|
||||
if (board[row][col] == 'O') board[row][col] = 'X';
|
||||
else if (board[row][col] == 'A') board[row][col] = 'O';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
@ -161,6 +161,35 @@ public:
|
||||
|
||||
## Java
|
||||
|
||||
```Java
|
||||
// 深度优先遍历
|
||||
class Solution {
|
||||
List<List<Integer>> ans; // 用来存放满足条件的路径
|
||||
List<Integer> cnt; // 用来保存 dfs 过程中的节点值
|
||||
|
||||
public void dfs(int[][] graph, int node) {
|
||||
if (node == graph.length - 1) { // 如果当前节点是 n - 1,那么就保存这条路径
|
||||
ans.add(new ArrayList<>(cnt));
|
||||
return;
|
||||
}
|
||||
for (int index = 0; index < graph[node].length; index++) {
|
||||
int nextNode = graph[node][index];
|
||||
cnt.add(nextNode);
|
||||
dfs(graph, nextNode);
|
||||
cnt.remove(cnt.size() - 1); // 回溯
|
||||
}
|
||||
}
|
||||
|
||||
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
|
||||
ans = new ArrayList<>();
|
||||
cnt = new ArrayList<>();
|
||||
cnt.add(0); // 注意,0 号节点要加入 cnt 数组中
|
||||
dfs(graph, 0);
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
```python
|
||||
class Solution:
|
||||
@ -192,3 +221,4 @@ class Solution:
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user