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. ubuntu tengine 安装

    参考文章:http://wangyan.org/blog/install-openssl-from-source.html http://www1.site90.com/Linux/405.html ...

  2. jsp 嵌套iframe 从iframe中表单提交并传值到外层

    今天因需求迭代 更改元来代码 遇到了这么个问题 就是想在 iframe中提交后进行整个页面的跳转 并把iframe中的值传到外层jsp 大概就是这个样子 外层 a.jsp <div id=&qu ...

  3. JavaScript—W3school

    一.JavaScript基础 1.写入HTML输出 2.对事件作出反应 3.改变HTML内容 4.改变HTML图像 5.改变HTML样式 6.验证输入 <script> Function ...

  4. 快速理解webStroage

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. HTML 事件

    1.HTML 全局事件属性 HTML4 的新特性之一就是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript,在 HTML5 中还增加了一些新 ...

  6. nginx Engine X静态网页服务器介绍

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. 反向代理(Reverse Proxy)方 ...

  7. windows下的SASS/Compass的安装与卸载

    认识SASS/Compass SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. SASS与Compass的安装说明 SASS在Wind ...

  8. 网易邮箱前端Javascript编码规范:基础规范

    在多年开发邮箱webmail过程中,网易邮箱前端团队积累了不少心得体会,我们开发了很多基础js库,实现了大量前端效果组件,开发了成熟的opoa框架以及api组件,在此向大家做一些分享.今天想先和大家聊 ...

  9. Navicat Premium 未保存的SQL如何找回 ?

    在使用 Navicat Premium 编辑SQL的过程中为防止程序意外崩溃,已经将编辑的SQL都已经备份. 备份存放目录地址:C:\Users\{登录用户名}\Documents\Navicat\M ...

  10. 面试题:实现LRUCache::Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...