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 ...
随机推荐
- set -- $variable
1 set --的用途 给位置参数赋值. 2 $variable是如何赋值给位置参数的 假如variable=a b c?或者variable=a;b;c? 这里果然和IFS有关,默认情况下,vari ...
- h5 getUserMedia error PermissionDeniedError
HTML5 在使用非 localhost 地址访问时打开摄像头失败 .报getUserMedia error PermissionDeniedError,火狐下是可以正常调取的. 需要https: 火 ...
- 文件批量转换成UTf-8
yum install -y enca 在文件夹根目录下面创建文件:iconv_shell.sh 里面填写下面的内容: #!/bin/bash for file in `find ./ -name ' ...
- sjtu oj 1201. SuperXOR
Description Pangzi recently realized that bitwise XOR operation is just an addition without carries. ...
- [RK3288][Android6.0] 调试笔记 --- 系统识别不同硬件版本方法【转】
本文转载自:http://m.blog.csdn.net/kris_fei/article/details/70226451 Platform: RockchipOS: Android 6.0Kern ...
- 解决post乱码之web.xml
<!-- 解决post乱码 --> <filter> <filter-name>characterEncodingFilter</filter-name> ...
- 【转载】U3D 游戏引擎之游戏架构脚本该如何来写
原文:http://tech.ddvip.com/2013-02/1359996528190113.html Unity3D 游戏引擎之游戏架构脚本该如何来写 2013-02-05 00:48:4 ...
- Opencv中视频播放与进度控制
视频画面本质上是由一帧一帧的连续图像组成的,播放视频其实就是在播放窗口把一系列连续图像按一定的时间间隔一幅幅贴上去实现的. 人眼在连续图像的刷新最少达到每秒24帧的时候,就分辨不出来图像间的闪动了,使 ...
- Android Studio自动生成带系统签名的apk
介绍签名的两种方式: 1.signapk.jar命令行方式: 如果你需要开发一个带有系统权限的app,往往需要配置SharedUserId,比如: </pre><pre name=& ...
- C:static 关键字、静态变量、跨类访问数据
static 在OC中的使用 参考1 参考2 参考3 参保4 参考5 跨类访问成员 参考 +号方法就是类方法(静态方法),说明不用创建对象,可以直接通过类型去直接调用这个方法,在OC ...