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 ...
随机推荐
- sanic官方文档解析之ssl,debug mode模式和test(测试)
1,ssl 示例: 可选择的SSLContent from sanic import Sanic import ssl context = ssl.create_default_context(pur ...
- 使用libcurl的包装库cpr发起http请求
cpr GitHub地址https://github.com/whoshuu/cpr 简单示例:cpr_http_request.cpp #include <iostream> #incl ...
- Hive JOIN的基本操作 及 内部实现
1.HIVE基本操作: [一起学Hive]之十一-Hive中Join的类型和用法 注:HIve不支持非等值连接: 什么是等值连接: //Oracle SQL 不等值连接 //通过不等值连接查找7788 ...
- 初探linux子系统集之led子系统(二)【转】
本文转载自:http://blog.csdn.net/eastmoon502136/article/details/37606487 巴西世界杯,德国7比1东道主,那个惨不忍睹啊,早上起来看新闻,第一 ...
- Eclipse慢慢学会的快捷键
Java编辑器 添加单个import Ctrl+Shift+M Java编辑器 组织多个import Ctrl+Shift+O Ctrl+M切换窗口的大小 Ctrl+D删除当前行 ---------- ...
- HDU6028:Forgiveness(TLE ing,占位)
Problem Description Little Q is now checking whether string A matches B. Two strings are considered ...
- [USACO 2016Dec] Team Building
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4742 [算法] 动态规划 用Fi,j,k表示约翰的前i头牛和保罗的前j头牛匹配 , ...
- 【ZJOI 2002】 昂贵的聘礼
[题目链接] 点击打开链接 [算法] 最短路,注意不能用dijkstra,要用SPFA [代码] #include <algorithm> #include <bitset> ...
- Map集合的几种遍历方式
Map<String ,String> map=new HashMap<String,String>(); map.put("1","value1 ...
- Android 业务组件化开发实践
组件化并不是新话题,其实很早很早以前我们开始为项目解耦的时候就讨论过的.但那时候我们说的是功能组件化.比如很多公司都常见的,网络请求模块.登录注册模块单独拿出来,交给一个团队开发,而在用的时候只需要接 ...