思路完全一样

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. Java - 尚学堂第八章常用类(将输入的string类型的值转为整数、浮点型、日期类型)

    import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...

  2. mysql 03

    CREATE TABLE class(    empno INT,    ename VARCHAR(4),    job VARCHAR(4),    mgr INT,    hiredate DA ...

  3. 关于Sql注入的那些事

    登陆注册应该是每一个网站的必做的业务,但是在选择使用Django中的ORM还是说执行原生的Sql语句不同的人应该会有不同的建议,有经验的开发人员都喜欢原生的sql语句,因为相对于ORM来说,执行效率高 ...

  4. 文件IO(存取.txt文件)

    //存文件方法 public void Save_File_Info(string Save_Path) { //根据路径,创建文件和数据流 FileStream FS = new FileStrea ...

  5. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  6. 禅道 xampp中的mysql启动时无法产生err文件

       使用opt/lampp/lampp start命令启动 错误提示:root@zabbix_server ~]# /opt/lampp/bin/mysqld_safe_helper: Can‘t ...

  7. 深入理解JavaScript系列(41):设计模式之模板方法

    介绍 模板方法(TemplateMethod)定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 模板方法是一种代码复用的 ...

  8. shell程序设计小知识

    一.用户登陆进入系统后的系统环境变量:$HOME 使用者自己的目录$PATH 执行命令时所搜寻的目录$TZ 时区$MAILCHECK 每隔多少秒检查是否有新的信件$PS1 在命令列时的提示号$PS2 ...

  9. C#把大写英文变成小写英文,把小写英文变成大写英文

    static void Main(string[] args) { string s;  // 声明一个变量,来接受用户输入的值. Console.WriteLine("请输入一个字符串:& ...

  10. 智能提示含查询多列(html+JS+handler+ HttpRemoting)二、Remoting代码

    /// <summary> /// 智能查询类型 /// </summary> public enum QueryType : byte { /// <summary&g ...