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. leetcode problem 6 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  2. 构建 struts2 spring3 mybatis 的maven项目 构建 pom.xml

    学习maven项目时 搭建个ssm项目 算是给自己留个备份吧 环境说明: MyEclipse10 Maven   3.2.3 框架: struts2    2.3.24.1 spring3    3. ...

  3. 【转】JSONP简介

     原文链接:说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 先说说JSONP是怎么产生的: 1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面. ...

  4. html meta标签用法详细介绍

    meta是html语言head区的一个辅助性标签. 在页面中都有类似这样的html代码: <head> <meta http-equiv="content-Type&quo ...

  5. 2014年度辛星html教程夏季版第二节

    上面一节中我们介绍了HTML文件的书写和几个标签,接下来我们来认识几个其他的标签,这里我们主要介绍一下head标签和文本标签. ***************head标签*************** ...

  6. 开发C# .net时使用的数据库操作类SqlHelp.cs

    练习开发WPF程序的时候,是这样写的,虽然很简单,相必很多新手会用到,所以拿来共享一下, using System; using System.Collections.Generic; using S ...

  7. App评分

    //应用实现评论跳转的两种方法: //第一种: //在iOS6.0前跳转到AppStore评分一般是直接跳转到AppStore评分 //NSString *evaluateString = [NSSt ...

  8. 设置UIButton的文字居右显示 去掉点击默认置灰效果

    1.设置UIButton的文字居右显示 [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; ...

  9. [CC150] Find a line passing the most number of points

    Problem: Given a two-dimensional graph with points on it, find a line which passes the most number o ...

  10. BZOJ 1692: [Usaco2007 Dec]队列变换

    Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的"全美农场主大奖赛".在这场比赛中,每个参赛者都必须让他的奶牛排成一列 ...