思路完全一样

AC的代码:

 class Solution {
private:
struct Point {
int x, y;
Point(int _x, int _y):x(_x), y(_y) {}
};
public:
void solve(vector<vector<char> > & board) {
const int M = board.size();
if (M <= ) return;
const int N = board[].size();
vector<Point> run; // 没被包含的O,判断后修改为D来标记
for (int i=; i<M; ++i) // 1 边界
for (int j=; j<N; ++j)
if ((i== || i==M- || j== || j==N-) && board[i][j]=='O') {
board[i][j] = 'D';
run.push_back(Point(i, j));
} const static int PATH[][] = {{,},{,-},{-,},{,}};
while (!run.empty()) { // 2 out -> insider
Point p = run.back();
run.pop_back();
for (int i=; i<; ++i) {
int x = p.x+PATH[i][];
int y = p.y+PATH[i][];
if (x< || x>=M || y< || y>= N || board[x][y]!='O')
continue;
board[x][y] = 'D';
run.push_back(Point(x, y));
}
} for (int i=; i<M; ++i) // 3 检查
for (int j=; j<N; ++j) {
if (board[i][j]=='X') continue;
board[i][j] = (board[i][j]=='O'?'X':'O');
}
}
};

时间通不过的代码:

 class Solution {
public:
void solve(vector<vector<char>> &board) {
int size = board.size();
if (size < )
return; for (int j = ; j < size; ++j) {
if (board[][j] == 'O') {
board[][j] == '';
judgeRegion(board, , j);
} if (board[size-][j] == 'O') {
board[size-][j] == '';
judgeRegion(board, size-, j);
}
} for (int i = ; i < size - ; ++i) {
if (board[i][] == 'O') {
board[i][] == '';
judgeRegion(board, i, );
} if (board[i][size-] == 'O') {
board[i][size-] == '';
judgeRegion(board, i, size - );
}
} for (int i = ; i < size; ++i) {
for (int j = ; j < size; ++j) {
if (board[i][j] == 'O')
board[i][j] = 'X';
if (board[i][j] == '')
board[i][j] == 'O';
}
}
} void judgeRegion(vector<vector<char>> &board, int i, int j) {
int size = board.size();
if (i >= && j < size && board[i][j] == 'O') {
board[i][j] == '';
judgeRegion(board, i-, j);
judgeRegion(board, i+, j);
judgeRegion(board, i, j-);
judgeRegion(board, i, j+);
}
} };

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. 推荐 IntelliJ IDEA 牛逼的插件

    1. activate-power-mode 和 Power mode II 根据Atom的插件activate-power-mode的效果移植到IDEA上 写代码是整个屏幕都在抖动,activate ...

  2. PHP 字符串 操作符<<< 使用的注意事项

    在看<深入PHP和JQeury开发>过程中,遇到字符串 操作符<<<,代码没有问题,但格式却要求特别严格,找到PHP手册上的实例,翻译过来,留作后用. A string ...

  3. js中的 == 与 === 、永远不要使用 ==

    前言: 很久没有复习基础了,所以导致做项目的时候被坑,咳咳,基础还是很重要的. === 是没有强制类型转换的,和其他大部分语言的 == 是一样的.而js中 == 是有类型转换的,这也是js饱受诟病的原 ...

  4. JSON中的坑

    坑一. 在使用localStorage时,我们会给一个key存取一个value,这个value可以是一个普通的字符串,也可以是一个对象,如果是一个字符串,我们就需要通过JSON.stringify来转 ...

  5. Git学习系列之Git和TortoiseGit的区别

    不多说,直接上干货! Git和TortoiseGit的区别: TortoiseGit的安装和使用依赖Git. Git有且只有一个,就是linux最初创建的那个叫做Git的程序.现在的维护者的名字我懒得 ...

  6. gradle简单配置跟模块依赖

    参考文章: https://www.cnblogs.com/lykbk/p/erwerwerwerwerwerwe.html https://www.cnblogs.com/jiangxiaoyaob ...

  7. Python基础(1) - 初识Python

    Python 特点: 1)面向对象 2)解释执行 3)跨平台.可移植 4)垃圾回收机制 5)动态数据类型.强类型 6)可扩展.可嵌入 Python可以方便调用C/C++等语言,同时也可以方便的被C/C ...

  8. codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】

    Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. asp get与post获取的区别

    1.HTTP请求格式: <request line> <headers> <blank line> [<request-body>] 在HTTP请求中, ...

  10. hexo&github博客搭建

    闲来无事,偶然看到hexo,便试着玩玩,hexo是一种静态博客工具,使用nodejs流生成静态博客,速度快,主题多,附地址:https://hexo.io/ 下面详细介绍如何使用hexo在github ...