[LeetCode]130. Surrounded Regions
来源:互联网 发布:sql 修改 primary key 编辑:程序博客网 时间:2024/05/22 13:46
https://leetcode.com/problems/surrounded-regions/
目前做到过的union find的题目都是两解,并查集或者bfs+递归。
解法一:并查集
注意一种特殊情况会stackoverflow:
OOOOOOOOOOXXXXXXXXXOOOOOOOOOOOOXXXXXXXXXOOOOOOOOOOXXXXXXXXXOOOOOOOOOOOOXXXXXXXXXOOOOOOOOOOXXXXXXXXXO
public class Solution { public void solve(char[][] board) { if (board == null || board.length == 0 || board[0].length == 0) { return; } for (int i = 0; i < board.length; i++) { dfs(board, i, 0); dfs(board, i, board[0].length - 1); } for (int i = 0; i < board[0].length; i++) { dfs(board, 0, i); dfs(board, board.length - 1, i); } for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (board[i][j] == '1') { board[i][j] = 'O'; } else if (board[i][j] == 'O') { board[i][j] = 'X'; } } } } int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; private void dfs(char[][] board, int x, int y) { if (board[x][y] == 'O') { board[x][y] = '1'; for (int[] dir : dirs) { int row = dir[0] + x; int col = dir[1] + y; // 为避免stackoverflow,因此row < board.length - 1, col < board[0].length - 1 if (row >= 0 && row < board.length - 1 && col >= 0 && col < board[0].length - 1 && board[row][col] == 'O') { dfs(board, row, col); } } } }}
解法二:并查集,保存两个数组id和是否连通最外边。并查集总是把二维转化成一维数组。顺序便利的时候union当前位置左侧和上方的相同元素的位置。最后遍历一遍,如果当前位置的root不与最外边联通,那就置为X,否则不动。
public class Solution { boolean[] hasEdge; int[] unionSet; public void solve(char[][] board) { if (board == null || board.length == 0 || board[0].length == 0) return; int height = board.length, width = board[0].length; hasEdge = new boolean[height * width]; unionSet = new int[hasEdge.length]; for (int i = 0; i < unionSet.length; i++) { unionSet[i] = i; } for (int i = 0; i < unionSet.length; i++) { int x = i / width, y = i % width; hasEdge[i] = (board[x][y] == 'O' && (x == 0 || x == height - 1 || y == 0 || y == width - 1)); } for (int i = 0; i < unionSet.length; i++) { int x = i / width, y = i % width, up = x - 1, left = y - 1; if (up >= 0 && board[up][y] == board[x][y]) union(i, i - width); if (left >= 0 && board[x][left] == board[x][y]) union(i, i - 1); } for (int i = 0; i < unionSet.length; i++) { int x = i / width, y = i % width; if (board[x][y] == 'O' && !hasEdge[findSet(i)]) { board[x][y] = 'X'; } } } private void union(int i, int j) { int rootX = findSet(i); int rootY = findSet(j); boolean temp = hasEdge[rootX] || hasEdge[rootY]; unionSet[rootX] = rootY; hasEdge[rootY] = temp; } private int findSet(int i) { if (unionSet[i] == i) return i; unionSet[i] = findSet(unionSet[i]); return unionSet[i]; }}
0 0
- [LeetCode]130.Surrounded Regions
- LeetCode 130. Surrounded Regions
- [leetcode] 130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- [LeetCode]130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- LeetCode *** 130. Surrounded Regions
- Leetcode 130. Surrounded Regions
- [leetcode] 130. Surrounded Regions
- LeetCode-130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- [LeetCode] 130. Surrounded Regions
- leetcode-- 130. Surrounded Regions
- 【LeetCode】130. Surrounded Regions
- Leetcode-130. Surrounded Regions
- python快速实现内建http服务器
- 使用 acl 库编写高并发非阻塞网络通信程序
- windows中链接远端服务器中的Oracle相关问题总结
- oracle中distinct的用法详解
- 【Mybatis架构】输入、输出映射
- [LeetCode]130. Surrounded Regions
- 1.1Maven简介
- linux下的开源文件恢复工具
- Qt在pro文件中加入带空格的路径
- Delphi调用SQL分页存储过程实例
- 栈操作
- Android杂谈--Activity、Window、View的关系
- 跟从别人的脚步,学习machine-learning
- 解决使用Genymotion出现Unable to start the virtual device.VirtualBox cannot start the virtual device.问题