原题链接https://leetcode.com/problems/surrounded-regions/

题目描述

Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.

A region is captured by flipping all 'O’s into 'X’s in that surrounded region.

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

Explanation:

Surrounded regions shouldn’t be on the border, which means that any ‘O’ on the border of the board are not flipped to ‘X’. Any ‘O’ that is not on the border and it is not connected to an ‘O’ on the border will be flipped to ‘X’. Two cells are connected if they are adjacent cells connected horizontally or vertically.

思路分析

把二维矩阵里的中间的O变成X,四周的X保留。
把四周的O标记,暂时记为T,然后把O变成X,T变回O,bingo

源码附录

class Solution {
public void solve(char[][] board) {
if(board == null || board.length == 0 || board[0].length == 0)
{
return;
} int col = board[0].length;
int row = board.length; for(int i=0;i<col;i++)
{
if(board[0][i] == 'O'){
dfs(board,0,i);
}
if(board[row-1][i] == 'O')
{
dfs(board,row-1,i);
}
}
for(int i=0;i<row;i++)
{
if(board[i][0] == 'O')
{
dfs(board,i,0);
}
if(board[i][col-1] == 'O')
{
dfs(board,i,col-1);
}
} for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(board[i][j] == 'T')
{
board[i][j] = 'O';
}
else if(board[i][j] == 'O')
{
board[i][j] = 'X';
} }
}
} public void dfs(char[][] board,int m,int n)
{
if(n < 0 || n >= board[0].length || m < 0 || m >= board.length || board[m][n] != 'O')
{
return;
} board[m][n] = 'T';
dfs(board,m -1,n);
dfs(board,m + 1,n);
dfs(board,m,n-1);
dfs(board,m,n+1);
}
}

Surrounded Regions——LeetCode进阶路的更多相关文章

  1. Surrounded Regions——LeetCode

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. Surrounded Regions leetcode java

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

  3. Surrounded Regions - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...

  4. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  5. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  6. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  7. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

  8. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  9. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  10. 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 用了第一种方式, ...

随机推荐

  1. pnpm : 无法加载文件 \AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本。

    1. 安装pnpm npm install -g pnpm #安装 pnpm pnpm --version #查看pnpm版本 安装完成后查看版本时报错 pnpm : 无法加载文件 C:\Users\ ...

  2. 面试题53 - I. 在排序数组中查找数字 I

    地址:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/ <?php /** 面试题53 ...

  3. deepseek: php测试代码执行用时

    在 PHP 中,你可以使用 microtime() 函数来测量代码的执行时间.microtime() 函数返回当前 Unix 时间戳的微秒数.你可以在代码的开始和结束处分别调用 microtime() ...

  4. C# Quartz 调度任务辅助类

    1 public class QuartzHelper 2 { 3 /// <summary> 4 /// 时间间隔执行任务 5 /// </summary> 6 /// &l ...

  5. 最新版 Proteus 8.15 Professional 图文安装教程(附安装包)

    前言 大家好,我是梁国庆. Proteus 是世界上唯一将电路仿真软件.PCB设计软件和虚拟模型仿真软件三合一的设计平台. 本篇博主将手把手带领大家安装最新版 Proteus 8.15. 若图片加载超 ...

  6. Go是怎么解决包依赖管理问题的?

    我们先来了解一下 Go 构建模式的演化过程,弄清楚 Go 核心开发团队为什么要引入 Go module 构建模式. Go 构建模式时怎么演化的? Go 程序由 Go 包组合而成的,Go 程序的构建过程 ...

  7. 解决 Docker 日志文件太大的问题

    Docker 在不重建容器的情况下,日志文件默认会一直追加,时间一长会逐渐占满服务器的硬盘的空间,内存消耗也会一直增加,本篇来了解一些控制日志文件的方法. 清理单个文件 运行时控制 全局配置 Dock ...

  8. Crealens.ai 免费体验GPT-4o 生图+吉卜力风格化

    自己的一张日常照片,能在几秒内变身为一幅充满童话感的吉卜力风插画?90%的人不知道,如今只需一句话,就能在 ChatGPT 里实现"AI 生图"--这就是 GPT-4o 生图功能的 ...

  9. Linux C线程读写锁深度解读 | 从原理到实战(附实测数据)

    Linux C线程读写锁深度解读 | 从原理到实战(附实测数据) 读写锁练习:主线程不断写数据,另外两个线程不断读,通过读写锁保证数据读取有效性. 代码实现如下: #include <stdio ...

  10. Vue的前端项目开发环境搭建

    一.本机window端:安装Node.js,其实质性功能相当于,java的maven https://nodejs.org/en/download/ 二.本机window端:检查Node.js的版本 ...