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

Follow up:

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 。

我的方案满足补充内容的第二点,使用 O(m + n) 额外空间。

  • 将矩阵中所有 0 元素的所在行,列记录下来。
  • 将记录下来的行号,整行设为0,将记录下来的列号,整列设为0.
 void setZeroes(vector<vector<int>>& matrix) {

     unordered_set<int> seti;
unordered_set<int> setk; for (int i = ; i < matrix.size(); i++) {
for (int k = ; k < matrix[].size(); k++) {
if (matrix[i][k] == ) {
seti.insert(i);
setk.insert(k);
}
}
} unordered_set<int>::iterator s_iter;
for (s_iter = seti.begin(); s_iter != seti.end(); s_iter++) {
for (int k = ; k < matrix[].size(); k++) {
matrix[*s_iter][k] = ;
}
} for (s_iter = setk.begin(); s_iter != setk.end(); s_iter++) {
for (int i = ; i < matrix.size(); i++) {
matrix[i][*s_iter] = ;
}
}
}

[LeetCode] 73. Set Matrix Zeroes 解题思路的更多相关文章

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

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

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

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

  3. [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 ...

  4. Leetcode#73 Set Matrix Zeroes

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

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

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

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

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

  8. Java for LeetCode 073 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. 解题思路: ...

  9. [LeetCode] 76. Minimum Window Substring 解题思路

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

随机推荐

  1. firefox下对ajax的onreadystatechange的支持情况分析及解决

    一.问题: var xmlHttp; function savecarttodata(){ createXMLHttpRequest(); var rndcode = new Date().getTi ...

  2. Python开发之--前端 HTML基础

    一:HTML介绍 HTML:超文本标记语言,标准通用标记语言下的一个应用.包括"头"部分(英语:Head).和"主体"部分(英语:Body),其中"头 ...

  3. linux服务器监控流量sh脚本

    服务器可能经常遇到服务器出带宽跑满,不知如何查询被哪个进程占用的情况,有一款开源的英文软件iftop功能比较强大可以查询相关信息,可能刚接触linux系统的朋友不太会使用,在此写了一个功能比较简单无需 ...

  4. zend studio 9.0.4 安装破解

    转载于 http://www.geekso.com/ZendStudio9-key 注册破解步骤第一步:如果已经安装过Zend Studio 9.0.4的,请打开Zend Studio 9.0.4,在 ...

  5. Spring和Hibernate相遇

    Spring是一个很贪婪的家伙,看到他的长长的jar包列表就知道了,其实对于hibernate的所有配置都是可以放在Spring中来进行得,但是我还是坚持各自分明,Spring只是负责自动探测声明类( ...

  6. android中监听layout布局

    android开发可以对layout文件夹中的xml文件里的布局进行监听,并处理事件,如:对RelativeLayout,LinearLayout,FrameLayout,GridLayout等布局容 ...

  7. 【Python网络爬虫三】 爬去网页新闻

    学弟又一个自然语言处理的项目,需要在网上爬一些文章,然后进行分词,刚好牛客这周的是从一个html中找到正文,就实践了一下.写了一个爬门户网站新闻的程序 需求: 从门户网站爬取新闻,将新闻标题,作者,时 ...

  8. Java集合类操作优化总结

    清单 1.集合类之间关系 Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHas ...

  9. 第一次写Makefile文件

    test.c文件内容 #include <stdio.h> int main(int argc, char const *argv[]) { printf("hahahah\n& ...

  10. ANDROID_MARS学习笔记_S03_002_设置可见性及扫描蓝牙设备

    一.代码 1.xml(1)AndroidManifest.xml <uses-permission android:name="android.permission.BLUETOOTH ...