题目描述

现在有一个仅包含‘X’和‘O’的二维板,请捕获所有的被‘X’包围的区域
捕获一个被包围区域的方法是将被包围区域中的所有‘O’变成‘X’
例如
  1. X X X XX O O XX X O XX O X X

执行完你给出的函数以后,这个二维板应该变成:

  1. X X X XX X X XX X X XX O X X

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,

  1. X X X XX O O XX X O XX O X X

After running your function, the board should be:

  1. X X X XX X X XX X X XX 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的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

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

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

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

  3. 【leetcode】Surrounded Regions

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

  4. [LintCode] Surrounded Regions 包围区域

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

  5. 22. Surrounded Regions

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

  6. Surrounded Regions

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

  7. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

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

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

  9. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

随机推荐

  1. 正式班D5

    2020.10.10星期六 正式班D5 一.上节课复习 1.硬盘分类 ​ 1.机械磁盘 ​ io时间=平均寻道时间+平均延迟时间 ​ buffer:写缓冲区 ​ cache:都缓存 ​ 2.固态硬盘 ...

  2. 翻了翻element-ui源码,发现一个很实用的指令clickoutside

    前言 指令(directive)在 vue 开发中是一项很实用的功能,指令可以绑定到某一元素或组件,使功能的颗粒度更精细.今天在翻 element-ui 的源码时,发现一个还挺实用的工具指令,跟大伙分 ...

  3. 利用 JS 脚本实现网页全自动秒杀抢购

    利用 JS 脚本实现网页全自动秒杀抢购 倒计时页面: 倒计时未结束时,购买按钮还不能点击. 结束时,可以点击购买,点击后出现提示"付款成功" 展示效果 1.制作测试网页 首先我们来 ...

  4. pytest文档53-命令行实时输出错误信息(pytest-instafail)

    前言 pytest 运行全部用例的时候,在控制台会先显示用例的运行结果(.或F), 用例全部运行完成后最后把报错信息全部一起抛出到控制台. 这样我们每次都需要等用例运行结束,才知道为什么报错,不方便实 ...

  5. docker启动服务

    1 rabbitmq docker启动服务---------------rabbitmq 2 mysql docker启动服务---------------mysql 3 redis docker启动 ...

  6. lumen-ioc容器测试 (5)

    lumen-ioc容器测试 (1) lumen-ioc容器测试 (2) lumen-ioc容器测试 (3) lumen-ioc容器测试 (4) lumen-ioc容器测试 (5) lumen-ioc容 ...

  7. Ubuntu20.4安装

    官网下载镜像 https://releases.ubuntu.com/20.04/ubuntu-20.04-live-server-amd64.iso 挂载开装 选语言 选键盘 网络设置DHCP到地址 ...

  8. C++ std::thread 多线程中的异常处理

    环境: VS2019 包含头文件: #include <iostream>#include<thread>#include<exception> 线程函数采用try ...

  9. bzoj2539 丘比特的烦恼、黑书P333 (最优二分图匹配)

      丘比特的烦恼 题目描述 Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女, ...

  10. 想买保时捷的运维李先生学Java性能之 生存与毁灭

    一.判断对象是否存活     1.引用计数算法   给对象中添加一个引用计数器,每当有一个地方引用它时,计数器就加1:当引用失效时,计数器的值就减1:任何时刻计数器为0的对象是不可能再被使用的.引用计 ...