引言

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. react-native debug js remotely跨域问题

    react-native debug js remotely跨域问题 我们在安卓真机上调试react-native时,启用debug js remotely的时候,会出现跨域问题.这个时候我们只需要一 ...

  2. scrum立会报告+燃尽图(第二周第三次)

    此作业要求参考: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2248 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公 ...

  3. MacOS下安装BeautifulSoup库及使用

    BeautifulSoup简介 BeautifulSoup库是一个强大的python第三方库,它可以解析html进行解析,并提取信息. 安装BeautifulSoup 打开终端,输入命令: pip3 ...

  4. 软工实践 - 第三十次作业 Beta答辩总结

    福大软工 · 第十二次作业 - Beta答辩总结 组长本次博客作业链接 项目宣传视频链接 本组成员 1 . 队长:白晨曦 031602101 2 . 队员:蔡子阳 031602102 3 . 队员:陈 ...

  5. VUE AXIOS 跨域问题

    背景: 后台跨域使用通配符:context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); ...

  6. lintcode-414-两个整数相除

    414-两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11. 标签 二分法 思路 ...

  7. 查看sqlserver数据库的编码格式

    查询语句:SELECT  COLLATIONPROPERTY('Chinese_PRC_Stroke_CI_AI_KS_WS', 'CodePage'): 查询结果: 936 简体中文GBK 950 ...

  8. 树莓派无显示器、无网线,优盘(U盘)启动,远程桌面

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:树莓派无显示器.无网线,优盘(U盘)启动,远程桌面     本文地址:http://techi ...

  9. 上传web端——个人项目

    我用visual studio新建了一个web窗口,如图: 然后这里是系统自带的代码: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile ...

  10. 【第六周】关于beta测试组员评分标准的若干意见

    组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块 评分规则:简单的才是坠吼的,本组不想搞个大新闻,所以奉行极简的评分方式.每一个人交给组长一个排名,假如 ...