给定一个二维的矩阵,包含 'X' 和 'O'(字母 O), 找到所有被 'X' 围绕的区域。
并将区域里所有 'O'用 'X' 填充。
例如,
X X X X
X O O X
X X O X
X O X X
运行你的函数后,该区域应该是:
X X X X
X X X X
X X X X
X O X X
详见:https://leetcode.com/problems/surrounded-regions/description/

Java实现:

class Solution {
public void solve(char[][] board) {
if(board==null){
return;
}
for(int i=0;i<board.length;++i){
for(int j=0;j<board[i].length;++j){
if((i==0||i==board.length-1||j==0||j==board[i].length-1)&&board[i][j]=='O'){
helper(board,i,j);
}
}
}
for(int i=0;i<board.length;++i){
for(int j=0;j<board[i].length;++j){
if(board[i][j]=='O'){
board[i][j]='X';
}
if(board[i][j]=='$'){
board[i][j]='O';
}
}
}
}
private void helper(char[][] board,int i,int j){
if(board[i][j]=='O'){
board[i][j]='$';
if(i>0&&board[i-1][j]=='O'){
helper(board,i-1,j);
}
if(j<board[i].length-1&&board[i][j+1]=='O'){
helper(board,i,j+1);
}
if(i<board.length-1&&board[i+1][j]=='O'){
helper(board,i+1,j);
}
if(j>0&&board[i][j-1]=='O'){
helper(board,i,j-1);
}
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4555831.html

130 Surrounded Regions 被围绕的区域的更多相关文章

  1. 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 用了第一种方式, ...

  2. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

  3. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  4. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  5. 130. Surrounded Regions -- 被某字符包围的区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  7. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. 130. Surrounded Regions

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

  9. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. bash shell parameter expansion

    1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配 ...

  2. IOS开发,知识点小结,ios开发中经常使用的宏定义总结

    IOS开发,从应用跳转到用浏览器打开网页: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:// ...

  3. ABAP debug遇到问题

    新项目的系统,调试是老出现这个框不断弹出,一堆出来 都来不及关. 不确定是不是因为可用对话框不够的原因.

  4. 浏览器和服务器 对post get请求 url长度限制

    1. URL长度限制 2. Post数据的长度限制 3. Cookie的长度限制 1. GET  URL长度限制 在Http1.1协议中并没有提出针对URL的长度进行限制,RFC协议里面是这样描述的, ...

  5. mybatis使用序列批量插入数据

    mybatis只提供了单条数据的插入,要批量插入数据我们可以使用循环一条条的插入,但是这样做的效率太低下,每插入一条数据就需要提交一次,如果数据量几百上千甚至更多,插入性能往往不是我们能接受的,如下例 ...

  6. Swift(二)控制流

    要处理条件逻辑,使用 if 和 switch ,要处理循环逻辑,使用 for-in, for, while, 和 do-while .包着条件或者循环的括号可加可不加.处理逻辑体的花括弧是必须加的. ...

  7. asp+jQuery解决中文乱码

    1. [代码][ASP/Basic]代码 '在客户端使用javascript的escape()方法对数据进行编码,在服务器端使用对等的VbsUnEscape()对数据进行解码,同样在服务器端使用Vbs ...

  8. Objective-C - - 字符串与数字互相转换

    NSString *string = @"123"; // 1.字符串转int int intString = [string intValue]; // 2.int装字符串 NS ...

  9. highchart学习网址

    http://www.highcharts.me/api/index.html   

  10. 一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...