Set Matrix Zeroes

Total Accepted: 18139 Total
Submissions: 58671My Submissions

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

click to show follow up.

题意:给定矩阵,假设矩阵的某个位置为0。则把那一行那一列的全部元素都置为0

思路:用两个bool数组,分别记录应该把全部元素置为0的行和列

复杂度:时间O(m*n)。空间O(m+n)

void setZeroes(vector<vector<int> > &matrix){
if(matrix.empty()) return ;
int rows = matrix.size(), columns = matrix[0].size();
vector<bool> row(rows, false);
vector<bool> column(columns, false);
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
if(matrix[i][j] == 0){
row[i] = column[j] = true;
continue;
}
}
}
for(int i = 0; i < rows; ++i){
if(!row[i]) continue;
for(int j = 0; j < columns; ++j){
matrix[i][j] = 0;
}
}
for(int j = 0; j < columns; ++j){
if(!column[j]) continue;
for(int i = 0; i < rows; ++i){
matrix[i][j] = 0;
}
}
}

Leetcode 细节实现 Set Matrix Zeroes的更多相关文章

  1. 【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 ...

  2. leetcode第一刷_Set Matrix Zeroes

    这个题乍一看非常easy,实际上还挺有技巧的.我最開始的想法是找一个特殊值标记.遇到一个0,把他所相应的行列中非零的元素标记成这个特殊值.0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题 ...

  3. 【一天一道LeetCode】#73. Set Matrix Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】-- 73. Set Matrix Zeroes

    问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...

  5. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  6. 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 ...

  7. 【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. Fo ...

  8. LeetCode OJ:Set Matrix Zeroes(设置矩阵0元素)

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

  9. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

随机推荐

  1. ASP.NET - List<> 绑定 DropDownList

    代码: //声明泛型 List<category> inof = new List<category>();//二级分类 //声明类使用的对象类 public class ca ...

  2. 使用JDBC处理数据库大容量数据类型

    在本文将介绍如何使用JDBC操作MySQL数据库对于大容量数据类型的读取.在之前的博客中已经介绍了如何使用JDBC来操作数据库对各种数据的增删改查,那么大容量数据类型的数据操作又为何不同呢. 原因在于 ...

  3. python httpConnection详解

    模块urllib,urllib2,httplib的区别 httplib实现了http和https的客户端协议,但是在python中,模块urllib和urllib2对httplib进行了更上层的封装. ...

  4. HTTP实现长连接(TTP1.1和HTTP1.0相比较而言,最大的区别就是增加了持久连接支持Connection: keep-alive)

    HTTP实现长连接 HTTP是无状态的 也就是说,浏览器和服务器每进行一次HTTP操作,就建立一次连接,但任务结束就中断连接.如果客户端浏览器访问的某个HTML或其他类型的Web页中包含有其他的Web ...

  5. 禁用win7自己主动配置ipv4地址

    现象 一台新电脑,连了网线,没有dhcp,须要手动配置Ip. 配置了一个Ip后,发现ping网关不通. ipconfig 发现有2 个IP:  自己主动配置 IPv4 地址  . . . . . . ...

  6. 怎样配置PHP环境和安装Zendstdio编辑器

    想学习PHP好久了.苦于环境配置不好,一直感觉无从下手. 在网上找了个视频: 李炎恢PHP教程 第一章前3节给出了具体的配置的方法,即安装Apache和Zendstudio 10.5仅仅须要照着视频做 ...

  7. 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时

    五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...

  8. JSTL解析——002——core标签库01

    javaEE5之前的版本需要引用JSTL相关的jar包.tld文件等,JAEE5之后就不用这么麻烦了, 如果你的还是不能使用就去官网下载(jstl.jar和standard.jar)这两个jar包,将 ...

  9. iOS_时间相关

    计算两个时间之差,并以倒计时的方式显示: 比如:剩余XX天XX小时XX分XX秒 效果图例如以下: NSDate的分类: 使用场景: watermark/2/text/aHR0cDovL2Jsb2cuY ...

  10. [C#基础] 继承

    虚方法和覆写方法 虚方法可以使基类的引用访问"升至"派生类中 可以使用基类引用调用派生类的方法,只需满足下面的条件 派生类的方法和基类的方法有相同的签名和返回类型 基类的方法使用v ...