surrounded-regions leetcode C++
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
C++
class Solution {
void dfs(vector<vector<char> > &board,int row,int col){
if (board[row][col]=='O'){
board[row][col]='*';
if (row<board.size()-1) dfs(board,row+1,col);
if (col<board[0].size()-1) dfs(board,row,col+1);
if (row>1) dfs(board,row-1,col);
if (col>1) dfs(board,row,col-1);
}
}
public:
void solve(vector<vector<char>> &board) {
if (board.size()<1 || board[0].size()<1) return;
int row = board.size(),col = board[0].size();
for(int i=0;i<row;++i){
dfs(board,i,0);
dfs(board,i,col-1);
}
for(int i=0;i<col;++i){
dfs(board,0,i);
dfs(board,row-1,i);
}
for(int i=0;i<row;++i){
for(int j=0;j<col;++j){
if (board[i][j]=='O') board[i][j]='X';
if (board[i][j]=='*') board[i][j]='O';
}
}
}
};
surrounded-regions leetcode C++的更多相关文章
- Surrounded Regions——LeetCode
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Surrounded Regions leetcode java
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- Surrounded Regions - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)
Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 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 用了第一种方式, ...
随机推荐
- apache php RabbitMQ配置方式
确定自己的php版本号和位数,去pecl.php.net下载版本相应的rabbitmq扩展包, 以php5版本为例,在http://pecl.php.net/package/amqp里面选择php5对 ...
- 2.1Java基础
2.1.9面向对象的三大特性(携程): 封装:把一个对象的属性隐藏在对象内部,外部对象不能直接访问这个对象的内部信息.但是可以提供一些可以被外界访问的方法来操作属性.就比如我们常常创建一个类,把他的属 ...
- 字符串出现的topK问题
/** * return topK string * @param strings string字符串一维数组 strings * @param k int整型 the k * @return str ...
- postgres 基础SQL语句 增删改
查看已创建的数据库:select datname from pg_database; 查看所有数据库的详细信息:select * from pg_database 创建数据库:create datab ...
- sqlite3 import/export db sqlite 导入 导出 数据
export: $ sqlite3 xxx.db3 > .output xxx.sql >.dump > .q import: $ sqlite3 xxx.db3 > .rea ...
- django使用celery搭配redis配置定时任务
已经安装环境: Python3.6 django==2.1.8(用2.2.2需要升级sqlite3) 项目名称:ceshiproject APP名称:ceshi 第一步:centos7下首先安装r ...
- [转载]php连接postgreSQL数据库及其操作(php5,postgreSQL9)
数据库连接:dbconn.php<?php$conn = pg_connect("host=localhost port=5432 dbname=myd user=postgres p ...
- CF1119H-Triple【FWT】
正题 题目链接:https://www.luogu.com.cn/problem/CF1119H 题目大意 \(n\)个可重集,第\(i\)个里有\(x\)个\(a_i\),\(y\)个\(b_i\) ...
- pandas 基础命令
参考链接:https://github.com/rmpbastos/data_science/blob/master/_0014_Boost_your_Data_Analysis_with_Panda ...
- 前端规范之Git提交规范(Commitizen)
代码规范是软件开发领域经久不衰的话题,几乎所有工程师在开发过程中都会遇到或思考过这一问题.而随着前端应用的大型化和复杂化,越来越多的前端团队也开始重视代码规范.同样,前段时间,笔者所在的团队也开展了一 ...