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. c#后台弹出提示

    Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<script>alert ...

  2. 进阶:案例三: Upload File using WebDynpro

    1.节点创建,其中DATASOURCE存放uploadfile名称 2.layout布局 3.upload事件代码: method ONACTIONUPLOAD . DATA: lo_Node typ ...

  3. HTTPS的学习

    HTTPS的学习总结   HTTPS学习总结 简述 HTTPS对比HTTP就多了一个安全层SSL/TLS,具体就是验证服务端的证书和对内容进行加密. 先来看看HTTP和HTTPS的区别 我用AFN访问 ...

  4. Mfc资源消息的响应机制

    Mfc消息的响应机制 Mfc中有很多资源,如图标资源,菜单资源,工具栏资源等等:那么,资源是如何进行消息响应和消息映射的呢? 它们的流程是: 某种资源——对应的ID号——消息映射——响应函数的声明与实 ...

  5. uva 699

    #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #i ...

  6. Swift代码实现加载WEBVIEW

    let webview = UIWebView(frame:self.view.bounds) webview.bounds=self.view.bounds //远程网页 webview.loadR ...

  7. MySQL 採用Xtrabackup对数据库进行全库备份

    1,xtrabackup简单介绍 关于数据库备份以及备份工具.參考:http://blog.itpub.net/26230597/viewspace-1460065/,这里来介绍xtrabackup已 ...

  8. Eclipse中快捷键的使用

    1:引入包   ctrl+shift+o 2:对输入进行提示:Alt+/ 3: 全局搜索:crtrl + h 4:Eclipse创建方法快捷键Alt+shift+M 5:Eclipse创建局部变量快捷 ...

  9. hadoop 磁盘限额配置

    配置方法: 在 hdfs-site.xml 里配置如下参数,注意,那个 value 的值是配置该磁盘保留的DFS不能使用的空间大小,单位是字节. (如果多块硬盘,则表示为每块硬盘保留这么多空间) &l ...

  10. delphi中用代码实现注册Ocx和Dll(有点怪异,使用CallWindowProc来调用指定函数DllRegisterServer)

    在windows系统中,可以通过Regsvr32来实现注册ocx或者dl, 编程时,调用Regsvr32来注册,却不能正常执行.尤其是在Win7系统中,需要管理员身份才能运行. 使用下面的代码则能正常 ...