[Leetcode][第130题][JAVA][被围绕的区域][DFS][BFS]
【问题描述】[中等]
【解答思路】
1. 深度优先搜索
使用深度优先搜索实现标记操作。在下面的代码中,我们把标记过的字母 O 修改为字母 A。
复杂度
class Solution {int[] dx = {1, -1, 0, 0};int[] dy = {0, 0, 1, -1};public void solve(char[][] board) {int n = board.length;if (n == 0) {return;}int m = board[0].length;Queue<int[]> queue = new LinkedList<int[]>();for (int i = 0; i < n; i++) {if (board[i][0] == 'O') {queue.offer(new int[]{i, 0});}if (board[i][m - 1] == 'O') {queue.offer(new int[]{i, m - 1});}}for (int i = 1; i < m - 1; i++) {if (board[0][i] == 'O') {queue.offer(new int[]{0, i});}if (board[n - 1][i] == 'O') {queue.offer(new int[]{n - 1, i});}}while (!queue.isEmpty()) {int[] cell = queue.poll();int x = cell[0], y = cell[1];board[x][y] = 'A';for (int i = 0; i < 4; i++) {int mx = x + dx[i], my = y + dy[i];if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') {continue;}queue.offer(new int[]{mx, my});}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (board[i][j] == 'A') {board[i][j] = 'O';} else if (board[i][j] == 'O') {board[i][j] = 'X';}}}}
}
2. 广度优先搜索
以使用广度优先搜索实现标记操作。在下面的代码中,我们把标记过的字母 O 修改为字母 A
时间复杂度:O(N) 空间复杂度:O(1)
class Solution {int[] dx = {1, -1, 0, 0};int[] dy = {0, 0, 1, -1};public void solve(char[][] board) {int n = board.length;if (n == 0) {return;}int m = board[0].length;Queue<int[]> queue = new LinkedList<int[]>();for (int i = 0; i < n; i++) {if (board[i][0] == 'O') {queue.offer(new int[]{i, 0});}if (board[i][m - 1] == 'O') {queue.offer(new int[]{i, m - 1});}}for (int i = 1; i < m - 1; i++) {if (board[0][i] == 'O') {queue.offer(new int[]{0, i});}if (board[n - 1][i] == 'O') {queue.offer(new int[]{n - 1, i});}}while (!queue.isEmpty()) {int[] cell = queue.poll();int x = cell[0], y = cell[1];board[x][y] = 'A';for (int i = 0; i < 4; i++) {int mx = x + dx[i], my = y + dy[i];if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') {continue;}queue.offer(new int[]{mx, my});}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (board[i][j] == 'A') {board[i][j] = 'O';} else if (board[i][j] == 'O') {board[i][j] = 'X';}}}}
}
【总结】
1. 细节:
1.1 方向定义
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
1.2 边界判断
if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != ‘O’) {
continue;
}
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2.DFS BFS 思路不复杂 注意细节 多写几遍
转载链接:https://leetcode-cn.com/problems/surrounded-regions/solution/bei-wei-rao-de-qu-yu-by-leetcode-solution/
[Leetcode][第130题][JAVA][被围绕的区域][DFS][BFS]相关推荐
- 算法题目——被围绕的区域(dfs,bfs)
leetcode.130被围绕的区域 dfs解法: 深度优先遍历: 思路: 读取数据后 1.先将数据的四周进行bfs算法(因为只有与外围接触的点,才能不被包围) 算法执行中时,递归看看该点的上下左右有 ...
- [Leetcode][第889题][JAVA][根据前序和后序遍历构造二叉树][分治][递归]
[问题描述][中等] [解答思路] copyOfRange class Solution {public TreeNode constructFromPrePost(int[] pre, int[] ...
- [Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]
[问题描述][中等] [解答思路] public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) { ...
- [Leetcode][第78题][JAVA][子集][位运算][回溯]
[问题描述][中等] [解答思路] 1. 位运算 复杂度 class Solution {List<Integer> t = new ArrayList<Integer>(); ...
- [Leetcode][第79题][JAVA][单词搜索][DFS][回溯]
[问题描述][中等] [解答思路] 1. DFS繁琐版本 class Solution {public boolean exist(char[][] board, String word) {bool ...
- [Leetcode][第40题][JAVA][数组总和2][回溯][剪枝]
[问题描述][中等] [解答思路] 1. 减法 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Ar ...
- [Leetcode][第216题][JAVA][数组之和3][回溯]
[问题描述][中等] [解答思路] 回溯 剪树枝 当和超过n 或 个数超过k 1. 正向求和 优化前 class Solution {public List<List<Integer> ...
- [Leetcode][第39题][JAVA][组合总和][回溯][dfs][剪枝]
[问题描述][中等] [解答思路] 1. 回溯 import java.util.ArrayDeque; import java.util.ArrayList; import java.util.De ...
- [Leetcode][第77题][JAVA][组合][回溯]
[问题描述][中等] [解答思路] 1. 回溯 class Solution {List<List<Integer>> lists = new ArrayList<> ...
最新文章
- Windows客户端C/C++编程规范“建议”——指针
- 开启注册丨NeurlPS 2021论文预讲会议题全公开,4大主题和25场报告等你来
- 标准W3C盒子模型和IE盒子模型CSS布局经典盒子模型(转)
- 装机必备工具(普通家庭版)
- WayOs路由器WAN口帐号保存工具,可直接发送至邮箱,及保存接口VLAN号
- SAP Spartacus express checkout 设计
- 朱啸虎回应“美团滴滴合并”;小米成世界第四大手机制造商;Ant Design 3.26.1 发布 | 极客头条...
- JZ6-旋转数组的最小数字
- Springboot JpaRepository findOne() 方法报错
- zabbix 创建触发器
- 网上商城项目总结报告
- sqlitedev 注册码
- Consul删除服务
- 冰箱android10,智能手机 篇十:手机冻冰箱总共分几步?AGM推出金嗓子手机H2,超大声音超长待机...
- 硬件工程师-电阻知识大全
- 房地产软件解决方案供应商明源云在港交所主板上市
- css特效之水滴效果
- Linux面试题及答案
- NC15979 小q的数列
- 交互媒体专题设计------《The Wiley Handbook of Human Computer Interaction》
热门文章
- 硬件描述语言复习笔记
- myeclipse快捷生成代码块
- Android activity跳转动画,6种activity进入动画
- android RecyclerView EditText 取消自动聚焦
- 中南大学王斌计算机学院,中南大学 信息科学与工程学院,长沙 410083
- jsencrypt加密同一值返回不同密文_密码学原语如何应用?解析密文同态性的妙用...
- leetcode 701 二叉搜索树的插入操作 C++ 递归和迭代
- leetcode 112路径总和
- php上操作redis,PHP操作redis
- 【软件开发底层知识修炼】二 深入浅出处理器之二 中断的概念与意义