#leetcode#130. Surrounded Regions
来源:互联网 发布:高斯模糊算法 cpu消耗 编辑:程序博客网 时间:2024/05/24 00:05
iven a 2D board containing 'X'
and 'O'
(the letter O), capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
X X X XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX O X X
--------------------------------------------
思路是flood fill,先把四条边上的O染色,把相邻O变成‘1’, 然后再遍历一般即可
class Solution { public void solve(char[][] board) { if(board == null || board.length < 2 || board[0].length < 2) return; int m = board.length; int n = board[0].length; for(int i = 0; i < n; i++){ fillEdge(board, 0, i); fillEdge(board, m - 1, i); } for(int j = 0; j < m; j++){ fillEdge(board, j, 0); fillEdge(board, j, n - 1); } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == 'O'){ board[i][j] = 'X'; }else if(board[i][j] == '1'){ board[i][j] = 'O'; } } } } private void fillEdge(char[][] board, int row, int col){ if(board[row][col] != 'O'){ return; } int m = board.length; int n = board[0].length; LinkedList<Integer> queue = new LinkedList<>(); queue.offer(row * n + col); while(!queue.isEmpty()){ int idx = queue.poll(); int x = idx / n; int y = idx % n; if(x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'O'){ continue; } board[x][y] = '1'; queue.offer((x + 1) * n + y); queue.offer((x - 1) * n + y); queue.offer(x * n + y + 1); queue.offer(x * n + y - 1); } } }
阅读全文
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
- WebRTC各种资料
- 最近的一些计划
- socketIO服务器,安卓,js端使用代码
- 几篇有关CSS的文章
- 解决maven删除和重新安装的问题
- #leetcode#130. Surrounded Regions
- 【codeforce-510B】fox and two dots
- 字符串问题---添加最少字符使字符串整体都是回文字符串
- 利用fir.im提供的api接口实现android apk自动更新和手动更新
- URL中“#” “?” &“”号的作用
- spring boot banner的设置
- HDU6154 CaoHaha's staff(规律,2017中国大学生程序设计竞赛
- JMeter安装步骤
- Excel要不要“引”