作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/set-matrix-zeroes/description/

题目描述

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

Example 1:

Input:
[
[1,1,1],
[1,0,1],
[1,1,1]
] Output:
[
[1,0,1],
[0,0,0],
[1,0,1]
]

Example 2:

Input:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
] Output:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]

Follow up:

  1. A straight forward solution using O(mn) space is probably a bad idea.
  2. A simple improvement uses O(m + n) space, but still not the best solution.
  3. Could you devise a constant space solution?

题目大意

有一个二维矩阵,如果某一个位置出现了0,那么这个0所在的行和列全部转化成0. 原地操作。

解题方法

原地操作

最简单的方法就是用一个新的二维矩阵保存每个位置的结果,最后再放回原地了,题目说了这样不好。

一个简单的优化是,只需要保存每行和每列是否应该置为0,这样用了O(m+n)的空间,但是还有更好的解法。

我想了一个不用额外空间的,暴力的解法。时间上也勉强通过了。。

这个题和289. Game of Life比较像,289题只用判断8-连通,而这个题需要判断一整行。

我使用的方法是将每个位置的元素的二进制再增加一位,也就是说在末尾补上一个0或者1代表这个是否应该变成0或者不变。如果经过遍历之后,发现最后一位是0,那么把这个位置的数字变成0,如果最后一位是1,那么把这个位置的数字还原成原来的数字(右移一位)。

由于每个数字记录它的状态是在原地进行的,所以我说没有用到额外的空间。但这个题不好的地方在于,第一,没有说出MN的范围,让我很难判断这个解法能否通过;第二,没有说出matrix[m][n]的范围,不能判断左移一位之后,整数是否溢出(Python不存在这个问题)。

时间复杂度是O((M*N) * (M + N)),空间复杂度是O(1).

时间复杂度有(M + N)是因为,对于每个位置都去把这个行、列的所有数值进行了遍历来判断是否存在0,其实可以通过把结果保存到这个行列的第一个位置即可,降低了判断的时间复杂度。

代码如下:

class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
M, N = len(matrix), len(matrix[0])
# (old + new)
if matrix and matrix[0]:
for m in range(M):
for n in range(N):
matrix[m][n] = (matrix[m][n] << 1) + (1 if matrix[m][n] else 0)
for m in range(M):
for n in range(N):
if self.getPos(matrix, m, n) == 0:
matrix[m][n] = matrix[m][n] >> 1 << 1
for m in range(M):
for n in range(N):
if matrix[m][n] & 1 == 0:
matrix[m][n] = 0
else:
matrix[m][n] >>= 1 def getPos(self, matrix, m, n):
# return 0 means this place ==> 0; 1 ==> don't change
if matrix[m][n] == 0:
return 0
M, N = len(matrix), len(matrix[0])
if any((matrix[m][i] >> 1) == 0 for i in range(N)):
return 0
if any((matrix[i][n] >> 1) == 0 for i in range(M)):
return 0
return 1

这个解法本身是有问题的,如果左移超出INT最大值的时候。比如C++这个方法就过不了。测试用例果然有个INT最大值,这样左移就超出范围了,下面解法是错的。

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) return;
const int M = matrix.size(), N = matrix[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (matrix[i][j] != 0)
matrix[i][j] = 1 + (matrix[i][j] << 1);
}
}
for (int r = 0; r < M; ++r) {
for (int c = 0; c < N; ++c) {
if (matrix[r][c] == 0) {
for (int j = 0; j < N; ++j) {
matrix[r][j] >>= 1;
matrix[r][j] <<= 1;
}
for (int i = 0; i < M; ++i) {
matrix[i][c] >>= 1;
matrix[i][c] <<= 1;
}
}
}
}
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if ((matrix[i][j] & 1) == 1) {
matrix[i][j] >>= 1;
} else {
matrix[i][j] = 0;
}
}
}
}
};

新建数组

如果使用新的数组,把老数组拷贝一份,那么就可以在原数组上进行判断,在新数组上进行修改,也就是题目说的O(MN)的空间复杂度。

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) return;
vector<vector<int>> newM(matrix);
const int M = matrix.size(), N = matrix[0].size();
for (int r = 0; r < M; ++r) {
for (int c = 0; c < N; ++c) {
if (newM[r][c] == 0) {
for (int j = 0; j < N; ++j) {
matrix[r][j] = 0;
}
for (int i = 0; i < M; ++i) {
matrix[i][c] = 0;
}
}
}
}
}
};

队列

使用一个队列保存出现过0的位置,然后就可以再次遍历把所有的位置设置为0了。这里也可以使用其他的数据结构。

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) return;
const int M = matrix.size(), N = matrix[0].size();
queue<pair<int, int>> q;
for (int r = 0; r < M; ++r) {
for (int c = 0; c < N; ++c) {
if (matrix[r][c] == 0) {
q.push({r, c});
}
}
}
while (!q.empty()) {
auto p = q.front(); q.pop();
for (int j = 0; j < N; ++j) {
matrix[p.first][j] = 0;
}
for (int i = 0; i < M; ++i) {
matrix[i][p.second] = 0;
}
}
}
};

参考资料:

日期

2018 年 9 月 26 日 —— 美好的一周又快要过去了。。
2018 年 12 月 17 日 —— 周一要从早起开始

【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 73. Set Matrix Zeroes 解题思路

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

  2. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  3. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

  4. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  5. [LeetCode] 73. Set Matrix Zeroes 矩阵赋零

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

  6. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  8. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  9. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. Oracle-除了会排序,你对ORDER BY的用法可能一无所知!

    导读 为什么只有ORDER BY后面可以使用列别名 为什么不推荐使用ORDER BY后接数字来排序 为什么视图和子查询里面不能使用ORDER BY -- ​小伙伴们在进行SQL排序时,都能很自然的使用 ...

  2. 数仓day03-----日志预处理

    1. 为什么要构建一个地理位置维表(字典) 在埋点日志中,有用户的地理位置信息,但是原始数据形式是GPS坐标,而GPS坐标在后续(地理位置维度分析)的分析中不好使用.gps坐标的匹配,不应该做这种精确 ...

  3. 大数据学习day14-----第三阶段-----scala02------1. 元组 2.类、对象、继承、特质 3.函数(必须掌握)

    1. 元组 映射是K/V对偶的集合,对偶是元组的最简单的形式,元组可以装着多个不同类型的值 1.1 特点 元组相当于一个特殊的数组,其长度和内容都可变,并且数组中可以装任何类型的数据,其主要用处就是存 ...

  4. 零基础学习java------20---------反射

    1. 反射和动态代理 参考博文:https://blog.csdn.net/sinat_38259539/article/details/71799078 1.0 什么是Class: 我们都知道,对象 ...

  5. Shell学习(四)——shell中各种括号的作用

    参考博客: [1]shell中各种括号的作用().(()).[].[[]].{} [2]shell中的单层大/中/小括号.双层大中小括号.命令替换等 一.前言 目录 单括号() 双括号(( )) 单中 ...

  6. tomcat源码1

    Lifecycle:(接口) LifecycleBase:abstract:添加,删除Listener,各种init,start,stop,destory LifecycleMBeanBase:abs ...

  7. Copy constructor vs assignment operator in C++

    Difficulty Level: Rookie Consider the following C++ program. 1 #include<iostream> 2 #include&l ...

  8. mybatis-扩展

    分页插件 使用pageHelper参考官方https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse. ...

  9. InnoDB的行锁模式及加锁方法

    MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...

  10. linux系统的一些常用命令

    cd 进入某个目录 ifconfig 查看本机的ip cp (要复制的文件的位置) (要把文件复制的位置) ll 查看文件下,文件的操作权限 ls查看该文件夹下的有那些文件和文件夹 vi filena ...