leetcode21 surrounded regions
题目描述
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
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
class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty())
return;
int rows = board.size();
int cols = board[0].size();
if(rows==0 || cols==0)
return;
for(int j=0;j<cols;j++)
{
DFS(board, 0, j);
DFS(board, rows-1, j); } for(int i=0;i<rows;i++) { DFS(board, i, 0); DFS(board, i, cols-1); } for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) if(board[i][j] == 'O') board[i][j] = 'X'; for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) if(board[i][j] == '*') board[i][j] = 'O';
}
void DFS(vector<vector<char> > &board, int r, int c)
{
if(board[r][c] == 'O')
{
board[r][c] = '*'; int rows = board.size(); int cols = board[0].size(); if(r > 1) DFS(board, r-1, c); if(r < rows-2) DFS(board, r+1, c); if(c > 1) DFS(board, r, c-1); if(c < cols-2) DFS(board, r, c+1); } }
};
leetcode21 surrounded regions的更多相关文章
- [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】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 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 ...
随机推荐
- ASP。NET MVC警告横幅使用Bootstrap和AngularUI Bootstrap
Watch this script in action - demo 下载Source Code from GitHub 下载Source Code from CodeProject (1.1 MB) ...
- Netty之网络编程数据编码
一.概况 我们在进行网络编程中会把各种数据转换为byte数据以便能在网络上传输,最常见的网络字节序--Little-Endian和Big-Endian,也让好多初进网络编程的新手摸不着头脑,还有按位或 ...
- git pull设置为无需密码
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...
- Docker学习:(一)初识Docker
Docker(容器虚拟化技术)要点(秒级启动) Docker的WWH公式学习 What[是什么]. Why[为什么要用它]. How[怎么用] 1.Docker简介 (1)问题:为什么会有docker ...
- vscode 插件保存记录
- DE2资源集锦
1.The School of Electrical and Computer Engineering (ECE) at the Georgia Institute of Technology:htt ...
- 写给前端同学的C++入门教程(一):概述和环境搭建
说明:本人是前端er,因为最近对 UE4(一个游戏开发引擎)产生了兴趣,而这个引擎源开发游戏时需要用到 C++ ,所以就开始入坑 C++ 了.现将自己学习 C++ 的笔记整理并分享出来,以便一些想入门 ...
- python numpy输出排名
python numpy排序后输出排名 问题: 假设某班的成绩为: 姓名 成绩 名次 小红 95 小黑 67 小白 58 小绿 82 小蓝 76 小橙 79 小可爱 99 请根据表格,输出对应的名次 ...
- 面经手册 · 第14篇《volatile 怎么实现的内存可见?没有 volatile 一定不可见吗?》
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.码场心得 你是个能吃苦的人吗? 从前的能吃苦大多指的体力劳动的苦,但现在的能吃苦已经包括太 ...
- 微服务从nacos配置中心获得配置信息
一,安装nacos, 略 二,创建父工程和微服务工程 service1, service2,以idea为例 1, new -> project -> Maven -> 填写group ...