Surrounded Regions [未完成]
思路完全一样
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 [未完成]的更多相关文章
- [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 ...
随机推荐
- python3.6 for pygame安装
首先下载好文件: pygame下载网址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 找到pygame-1.9.2b8-cp36-cp36m-wi ...
- golang io.ReadFull
buf := make([]byte, 10, 10) file, _ := os.Open("./data.txt") n, err := io.ReadFull(file, b ...
- 如何使用新的glibc来编译自己的程序
http://www.sysnote.org/2015/08/25/use-new-glibc/ 通常情况下我们都是直接使用glibc提供的一些库函数,但是某些特殊的情况,比如要修改glibc的一些代 ...
- Linux下用tree命令列出树形图
命令用法: tree 参数: -a 显示所有文件和目录. -A 使用ASNI绘图字符显示树状图而非以ASCII字符组合. -C 在文件和目录清单加上色彩,便于区分各种类型. -d 显示目录名称而非内容 ...
- (转)linux exec与重定向
原文:http://xstarcd.github.io/wiki/shell/exec_redirect.html linux exec与重定向 exec和source都属于bash内部命令(buil ...
- React.js 小书 Lesson19 - 挂载阶段的组件生命周期(二)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson19 转载请注明出处,保留原文链接和作者信息. 这一节我们来讨论一下对于一个组件来说,cons ...
- Signal Handling--ref
http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_21.html A signal is a software interrupt d ...
- 关于windowsServer编程
1.关于windowsServer编程
- mybatis学习之分页
分页一般分为物理分页:先查询所有值再分页输出,逻辑分页:直接分页查询输出,mybatis支持物理分页,如下: 1.物理分页: mapper映射: <select id="findStu ...
- centos下mongodb安装
安装说明: 系统环境:Centos-6.5 安装软件:mongodb-linux-x86_64-2.4.9.tgz 下载地址:http://www.mongodb.org/downloads 上传位置 ...