引言

Matrix内部的值修改严格来讲放在一个系列里不大合适,因为对于不同的问题,所用的算法和技巧可能完全不同,权且这样归类,以后需要时再拆分吧。

例题 1

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,

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
class Solution {
public:
void solve(vector<vector<char>> &board) {
}
};

这道题的思路应该比较容易想到:

遍历最外层一圈,如果有O,就把其相邻的也设置为O。接着遍历全矩阵,把内层O置为X。

但是这样做的问题遍历全矩阵时,分不清遇到的O是内层还是外层。

因此改进的方法是遍历最外层时,将O及其相邻的字符都设为"Y"。遍历全矩阵时,把"O"设置为X,把"Y"设置回"O"。

寻找O的邻居时,用的自然是BFS。每遇到一个O,就通过BFS将它以及邻居设置为"Y"

代码:

class Solution {
struct Point{
int h;
int v;
Point(int vp, int hp) : v(vp), h(hp) {};
};
public:
void BFS(int startH, int startW, vector<vector<char>> &board, queue<Point> que){
while(!que.empty()) que.pop();
int W = board[].size(), H = board.size();
Point p(startH, startW);
que.push(p);
while(!que.empty()){
Point cur = que.front();
que.pop();
board[cur.v][cur.h] = 'Y';
for(int i = ; i < ; i++){ //扫描四个方向上的邻居
if((cur.v+addV[i]) < H
&& (cur.h+addH[i]) < W
&& (cur.v+addV[i]) >=
&& (cur.h+addH[i]) >=
&& board[cur.v+addV[i]][cur.h+addH[i]] == 'O'){
que.push(Point(cur.v+addV[i], cur.h+addH[i]));
}
}
}
} void solve(vector<vector<char>> &board) {
if(board.size() == || board[].size() == ) return;
int W = board[].size(), H = board.size();
queue<Point> que;
int i, j = ;
for(i = ; i < W; ++i){
if(board[][i] == 'O') BFS(, i, board, que);
if(H > && board[H-][i] == 'O') BFS(H-, i, board, que); //遇到'O',调用BFS
}
for(i = ; i < H; ++i){
if(board[i][] == 'O') BFS(i, , board, que);
if(W > && board[i][W-] == 'O') BFS(i, W-, board, que);
} for(i = ; i < H; ++i){ //再次遍历全数组
for(j = ; j < W; ++j){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'Y') board[i][j] = 'O';
}
}
}
private:
int addV[] = {, , -, };
int addH[] = {, , , -};
};

这样提交上去,超时。

有什么办法可以缩短时间呢?回想思路可以发现:每次遇到O,我们都调用一次BFS,每一次调用BFS,都需要清空队列que,然后再push。

为什么不把所有的BFS并为一次呢?每次遇到O,我们只向队列que中push当前O所在的Point,最后调用一次BFS集中处理que,其效果是完全一样的,但是时间上却省去了每次清空队列的时间。

代码:

class Solution {
struct Point{
int h;
int v;
Point(int vp, int hp) : v(vp), h(hp) {};
};
public:
void solve(vector<vector<char>> &board) {
if(board.size() == || board[].size() == ) return;
int W = board[].size(), H = board.size();
queue<Point> que;
int i, j = ;
for(i = ; i < W; ++i){
if(board[][i] == 'O') que.push(Point(, i));
if(H > && board[H-][i] == 'O') que.push(Point(H-, i)); //遇到O,只push,不再调用BFS
}
for(i = ; i < H; ++i){
if(board[i][] == 'O') que.push(Point(i, ));
if(W > && board[i][W-] == 'O') que.push(Point(i, W-)); //遇到O,只push,不再调用BFS
}
while(!que.empty()){ //调用BFS
Point cur = que.front();
que.pop();
board[cur.v][cur.h] = 'Y';
for(int i = ; i < ; i++){
if((cur.v+addV[i]) < H
&& (cur.h+addH[i]) < W
&& (cur.v+addV[i]) >=
&& (cur.h+addH[i]) >=
&& board[cur.v+addV[i]][cur.h+addH[i]] == 'O'){
que.push(Point(cur.v+addV[i], cur.h+addH[i]));
}
}
} for(i = ; i < H; ++i){
for(j = ; j < W; ++j){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'Y') board[i][j] = 'O';
}
}
}
private:
int addV[] = {, , -, };
int addH[] = {, , , -};
};

小结:

这种需要更改Matrix的值的题目,上面解法用到了很简单的技巧:“引入一个中间值 Y”,避免了混淆。

例题 2

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Could you devise a constant space solution?

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) { }
};

这道题的困难之处在于“需要空间复杂度为常量”

有了上一题的启发,我们可以将0所在的行和列都设置为别的值X,最后将所有X设置为0。

但是这样做的弱点在于:

1. 每个元素都要被访问 >= 2次 (那些和0同行同列的元素被访问大于2次,那些不和0同行同列的元素被访问了2次)。

2. 如果题目改成vector<vector<bool> >,这种解法就失效了,因为没有第三个值可以引入。

如果空间复杂度要求是O(m+n)的话,我们会申明两个数组,分别来记录需要设为0的行号和列号。

进一步,如果如果空间复杂度要求是O(1),我们虽然不能申明新数组,但是我们能用第一行和第一列来标记那些需要置为0的行和列。

当遇到 matix[i][j] == 0时,将matix[0][j] 和 matrix[i][0] 置为 0。

第一遍遍历matrix结束后,将所记录的行和列置为0。

这样做需要注意:如果第一行或者第一列有0,需要额外记录。

代码:

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.size() == ) return;
if(matrix[].size() == ) return; bool firstRowSet = false;
bool firstColSet = false;
int i, j;
for(i = ; i < matrix.size(); ++i){
for(j = ; j < matrix[i].size(); ++j){
if(matrix[i][j] == ){
if(i == ) firstRowSet = true;
if(j == ) firstColSet = true;
matrix[i][] = ;
matrix[][j] = ;
}
}
} for(i = ; i < matrix.size(); ++i){
if(matrix[i][] == ){
for(j = ; j < matrix[i].size(); ++j)
matrix[i][j] = ;
}
} for(j = ; j < matrix[].size(); ++j){
if(matrix[][j] == ){
for(i = ; i < matrix.size(); ++i)
matrix[i][j] = ;
}
} if(firstRowSet)
for(j = ; j < matrix[].size(); ++j)
matrix[][j] = ;
if(firstColSet)
for(i = ; i < matrix.size(); ++i)
matrix[i][] = ;
}
};

[LeetCode] Matrix 值修改系列,例题 Surrounded Regions,Set Matrix Zeroes的更多相关文章

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

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

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

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

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

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

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

  7. 【leetcode】Surrounded Regions

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

  8. LeetCode: Surrounded Regions 解题报告

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

  9. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

随机推荐

  1. Python3 函数式编程自带函数

    一 map函数 引子 需求1:num1=[1,2,3,4],我的需求是把num1中的每个元素平方后组成新列表. ret = [] num1 = [1,2,3,4] for i in num1: ret ...

  2. apply新用法,最大值查找

    要找到数组中的最大或最小值,可以像下面这样使用apply() var values=[1,2,3,4,5,6,7,8]; var max = Math.max.apply(Math,values); ...

  3. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem F. Format

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:1s 空间限制:512MB 题目大意: 给定一个字符串,使用%[...] ...

  4. MySQL 基于xtrabackup备份—热备工具

    xtrabackup(仅对InnoDB存储引擎支持热备) percona公司开发 改进的MySQL分支:percona-server 存储引擎改进:InnoDB —> XtraDB 使用本地的R ...

  5. wx import require的理解

    服务器端的Node.js遵循CommonJS规范.核心思想是允许模块通过require 方法来同步加载所要依赖的其他模块,然后通过 exports或module.exports来导出需要暴露的接口. ...

  6. phpisset()和empty()函数区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  7. Html5新增元素中Canvas 与内联SVG的比较!

    SVG与Canvas的区别与比较如下: svg:使用xml描述2D图形,canvas使用javascript描述2D图形. Canvas 是逐像素进行渲染的,在 canvas 中,一旦图形被绘制完成, ...

  8. iOS--开发从入门到精通

    前言: 从事iOS开发已有几个年头,平时对于iOS开发的知识积累都比较碎片化,为了更好的掌握开发技能, 索性整理iOS开发的知识体系,以便于后面进阶成iOS高级开发工程师. 一.iOS开发基础 开发设 ...

  9. 转---秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock

    在<秒杀多线程第十一篇读者写者问题>文章中我们使用事件和一个记录读者个数的变量来解决读者写者问题.问题虽然得到了解决,但代码有点复杂.本篇将介绍一种新方法——读写锁SRWLock来解决这一 ...

  10. 【二】shiro入门 之 身份验证

    大体步骤如下: 1.首先通过new IniSecurityManagerFactory 并指定一个ini 配置文件来创建一个SecurityManager工厂: 2.接着获取SecurityManag ...