130 Surrounded Regions 被围绕的区域
给定一个二维的矩阵,包含 '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 被围绕的区域的更多相关文章
- 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 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 130. Surrounded Regions -- 被某字符包围的区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 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
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- 我遇到的错误curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused
今天我用curl命令,无论如何都是出现: curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused 找了很久,不知道 ...
- React中Transition的作用
/** * `Transaction` creates a black box that is able to wrap any method such that * certain invarian ...
- ADB运行框架原理解析【转】
本文转载自:http://blog.csdn.net/wlwl0071986/article/details/50935496 一.adb守护进程的初始化 源码路径:~/system/core/adb ...
- CentOS7 安装和配置 mysql5.7
1.下载 mysql源安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.安装mysql源 ...
- Tomcat版本是32位、64位问题
最近遇到一个Tomcat windows安装版本是32位还是64位问题.由于一系列原因,已经无从知晓生产系统上的该程序是32位还是64位. 后来经过仔细查阅资料,得知: 1. tomcat 从6.0. ...
- [SoapUI] Read data from response , use it to update parameter
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...
- -webkit-text-size-adjust 处理设置字体<12px
-webkit-text-size-adjust 1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text- ...
- A - Soldier and Bananas
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description A sold ...
- View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views
Views Because view objects are the main way your application interacts with the user, they have many ...
- centos7添加新硬盘并挂载
一.查看现有磁盘设备 fdisk -l 发现/dev/sdb 为新加的硬盘: 二.开始分区 fdisk /dev/sdb fdisk -l #再次查看分区情况,已经有了/dev/sdb1 三.创建文件 ...