【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.
Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution?
提示:
这题要求用常数的空间去解决,那么我们就可以利用第一行和第一列的元素去标记该行或该列是否在更新时要全部变成0。但是单纯这样操作会使得第一行和第一列的原始状态丢失。
因此,我们需要额外一个变量去保存第一列(或者第一行也可以)在更新时是否要变成0,这样就不会有问题了。
另外还有一个让代码更加精简的小窍门,就是在更新的时候从下往上(Bottom-Up),这样就不用担心会破坏第一行或第一列的数据了。
代码:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int height = matrix.size();
int width = height ? matrix[].size() : ;
if (!height || !width) {
return;
}
int col0 = ;
for (int i = ; i < height; ++i) {
if (matrix[i][] == ) {
col0 = ;
}
for (int j = ; j < width; ++j) {
if (matrix[i][j] == ) {
matrix[i][] = ;
matrix[][j] = ;
}
}
}
for (int i = height - ; i >= ; --i) {
for (int j = width - ; j >= 1; --j) {
if (matrix[i][] == || matrix[][j] == ) {
matrix[i][j] = ;
}
}
if (col0 == ) {
matrix[i][] = ;
}
}
}
};
【LeetCode】73. Set Matrix Zeroes的更多相关文章
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
- 【LeetCode】-- 73. Set Matrix Zeroes
问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- LeetCode OJ 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. click ...
- 【leetcode】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
- 【LeetCode】766. Toeplitz Matrix 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...
- 【LeetCode】59. Spiral Matrix II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...
随机推荐
- 通过分析 JDK 源代码研究 TreeMap 红黑树算法实
TreeMap和TreeSet是Java Collection Framework的两个重要成员,其中TreeMap是Map接口的常用实现类,而TreeSet是Set接口的常用实现类.虽然HashMa ...
- 非阻塞式线程安全列表-ConcurrentLinkedDeque
一.ConcurrentLinkedDeque public class ConcurrentLinkedDeque<E> extends AbstractCollection<E& ...
- struts-config.xml的配置
1.<struts-config> 元素 <struts-cofnig> 元素是 Struts 配置文件的根元素.<struts-config> 元素有 8 个子 ...
- CI集成phpunit Error: No code coverage driver is available 的解决
CI集成phpunit时,运行报No code coverage driver is available的错误,如下图: yanglingdeMacBook-Pro:tests yangling$ p ...
- servlet的过滤器的doFilter()
doFilter中的chain.doFilter(res,req);//Filter 只是链式处理,请求依然转发到目的地址 意思就说过滤器只是一个关口,如果符合条件的请求会被过滤器拦截下来,然后进行处 ...
- iOS 证书详情介绍
1.Certification(证书)安装证书后使这台电脑就有开发APP和真机调试的功能. 证书分为开发证书(Developer Certification)和发布证书(Distribution Ce ...
- Python爬虫02——贴吧图片爬虫V2.0
Python小爬虫——贴吧图片爬虫V2.0 贴吧图片爬虫进阶:在上次的第一个小爬虫过后,用了几次发现每爬一个帖子,都要自己手动输入帖子链接,WTF这程序简直反人类!不行了不行了得改进改进. 思路: 贴 ...
- Js之浅谈dom操作
JavaScript之浅谈dom操作 1.理解dom: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两 ...
- Java 后台创建word 文档
---恢复内容开始--- Java 后台创建 word 文档 自己总结 网上查阅的文档 分享POI 教程地址:http://www.tuicool.com/articles/emqaEf6 方式一. ...
- 史上最完整Hadoop2.x完全分布式安装部署-小白也能学会
一.环境要求: 1. 虚拟机安装并设置网络: 2. 修改主机地址映射: 3. 必备软件:Jdk.Development Tools Development ...