Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and '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 X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
解题思路:
对最外面一圈进行BFS,替换掉最外圈的连通范围,剩下的‘O’都是被包围的,就可以直接kill掉,JAVA实现如下:
static public void solve(char[][] board) {
if (board.length <= 2 || board[0].length <= 2)
return;
for (int row = 0; row < board.length; row++) {
if (board[row][0] == 'O') {
board[row][0] = 'T';
bfs(board, row * board[0].length);
}
if (board[row][board[0].length - 1] == 'O') {
board[row][board[0].length - 1] = 'T';
bfs(board, row * board[0].length + board[0].length - 1);
}
}
for (int col = 1; col < board[0].length - 1; col++) {
if (board[0][col] == 'O') {
board[0][col] = 'T';
bfs(board, col);
}
if (board[board.length - 1][col] == 'O') {
board[board.length - 1][col] = 'T';
bfs(board, (board.length - 1) * board[0].length + col);
}
}
for (int row = 0; row < board.length; row++)
for (int col = 0; col < board[0].length; col++) {
if (board[row][col] == 'T')
board[row][col] = 'O';
else if (board[row][col] == 'O')
board[row][col] = 'X';
}
}
static public void bfs(char[][] board, int num) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(num);
while (!queue.isEmpty()) {
num=queue.poll();
int row = num / board[0].length;
int col = num - row * board[0].length;
if (row - 1 >= 0 && board[row - 1][col] == 'O') {
board[row - 1][col] = 'T';
queue.add(num - board[0].length);
}
if (row + 1 <= board.length - 1 && board[row + 1][col] == 'O') {
board[row + 1][col] = 'T';
queue.add(num + board[0].length);
}
if (col - 1 >= 0 && board[row][col - 1] == 'O') {
board[row][col - 1] = 'T';
queue.add(num - 1);
}
if (col + 1 <= board[0].length - 1 && board[row][col + 1] == 'O') {
board[row][col + 1] = 'T';
queue.add(num + 1);
}
}
}
Java for LeetCode 130 Surrounded Regions的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130 Surrounded Regions DFS
将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- 安卓Webview缓存网页数据(无网络正常显示)
热度 1已有 52 次阅读2016-8-26 17:53 |个人分类:常见问题|系统分类:移动开发 一.需求经历 最近的项目是一个原生 +webview 显示的 APP,一开始的时候,网站那边要求我们 ...
- 学习PHP:PHP提取的时间出现不准确
php函数date("Y-n-d H-i-s"); 输出的时间与当地时间居然相差了8个小时. 原因是从php5.1.0开始,php.ini里加入了date.time ...
- HDU 3150 Robot Roll Call – Cambot…Servo…Gypsy…Croooow(map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3150 Problem Description Mystery Science Theater 3000 ...
- checkbox 自动换行
把匹配的checkbox和文字用一对span标签包裹 并且给这个span标签加样式 display:inline-block <span style="display:inline-b ...
- windows8开发-关于wp7应用迁移到win8 metro风格
虽然微软说,wp7应用移植到win8上面是比较简单,只需要修改部分API和设计原则上的细节,同时它也提供了一份比较简洁的参考文档: 而实际上这种移植的工作量还是不小的,尤其当应用引用了较多底层的API ...
- mysql跨服务器查询
MySQL FEDERATED引擎使用示例, 类似Oracle DBLINK 摘要: 本地MySQL数据库要访问远程MySQL数据库的表中的数据, 必须通过FEDERATED存储引擎来实现. 有点类似 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 client对象模型(CSOM)基础
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--client对象模型和REST APIs概览 client对象模型(CSOM)基础 在SP2 ...
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental……
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to th ...
- Action window Flags
Action window 主要字段使用 含义 target 值 作用 current 当前窗口 new 新窗口 inline 内联编辑 fullscreen 全屏 main 当前窗口的主动作 ...
- 身份证号码 javascript 验证
function checkIsIdno(idcard) { var Errors=new Array( "SUCCESS", "身份证号码位数不对!", &q ...