题目:

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

分析:

题意很简单,就是把为0的元素所在的行和列都置0;

首先不能直接边循环边做,这样会造成很多本来不是0的元素被置0之后,在后续判断中将它的行列也置0;

所以简单的方法是建立两个数组,存行和列是否为0的情况,遍历一遍更新两个数组,再遍历一遍把应该置0的全部置0。

自己写的时候先考虑用了个set便于不记录重复的下标,所以有如下代码1:

 class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
set<int> rowflag;
set<int> colomnflag;
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (matrix[i][j] == ) {
rowflag.insert(i);
colomnflag.insert(j);
}
}
}
for (auto i : rowflag) {
for (int j = ; j < matrix[].size(); ++j) {
matrix[i][j] = ;
}
}
for (auto i : colomnflag) {
for (int j = ; j < matrix.size(); ++j) {
matrix[j][i] = ;
}
}
return;
}
};

程序还算简洁,记录最坏情况那里空间复杂度是为的O(nlogn + mlogm);

记录bool数组的方式,自己开始觉得写起来不够简洁。

后来看了讨论区大家的写法,发现第二次的用循环一遍,然后rowflag[i] || colonmnflag[j]可以很简洁的写出来,所以有如下代码2:

 class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rowflag[matrix.size()] = {};
int colomnflag[matrix[].size()] = {};
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (matrix[i][j] == ) {
rowflag[i] = ;
colomnflag[j] = ;
}
}
}
for (int i = ; i < matrix.size(); ++i) {
for (int j = ; j < matrix[].size(); ++j) {
if (rowflag[i] == || colomnflag[j] == ) {
matrix[i][j] = ;
}
}
}
return;
}
};

题目的follow-up中问道能否用常数的空间解决该问题,做法就是用两个变量记录第一行和第一列是否有0,

然后用第一行和第一列充当rowflag,colomnflag即可。

LeetCode73 Set Matrix Zeroes的更多相关文章

  1. Leetcode73. Set Matrix Zeroes矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  2. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

  3. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

  4. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

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

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

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

  8. [Swift]LeetCode73. 矩阵置零 | 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 ...

  9. 【leetcode】Set Matrix Zeroes(middle)

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

随机推荐

  1. uni-app开发的应用(小程序,app,web等),使用Node+Koa2开发的后端程序接收上传文件的方法

    uni-app使用使用Node+Koa2开发的后端程序接收上传的文件 通过gitbook浏览此随笔 通过其它客户端上传(h5,小程序等),接收方法一致 使用koa接收时,我们需安装一个中间件koa-b ...

  2. H5C3--仿京东首页(包含轮播图,倒计时)

    !!!很抱歉,之前的域名已经过期了,已经被别人购买了拿去做菠菜了,现在的话,京东页面我重新发布一下代码 #请看注意事项,因为有模拟请求,请在本地服务器或者IDEA类编译器打开(2019年10月10日0 ...

  3. bootstrap-fileinput详细说明与使用

    介绍 bootstrap-fileinput是一款非常优秀的HTML5文件上传插件,支持文件预览.多文件上传等一系列特性. 一款非常优秀的HTML5文件上传插件,支持bootstrap 3.x 和4. ...

  4. R语言的可视化

    1. 完整的数据分析流程 定义研究问题 定义理想数据集 确定能够获取什么数据 清理数据 2. 变量的类型: 数值变量(可进行加减乘除运算):连续(可在给定区间取任意数值).离散(给定集合内不连续取值) ...

  5. [Git高级教程(二)] 远程仓库版本回退方法 - 梧桐那时雨 - CSDN博客

    1 简介 最近在使用git时遇到了远程分支需要版本回滚的情况,于是做了一下研究,写下这篇博客. 2 问题 如果提交了一个错误的版本,怎么回退版本? 如果提交了一个错误的版本到远程分支,怎么回退远程分支 ...

  6. js实现HashTable

    1.哈希表使用键值对进行的数据储存,在数据的存储位置和它的关键字之间建立一一对应的关系,从而使关键字和结构中的一个唯一的存储位置相对应,所以在检索数据时 只需要根据这个关系便可以快速定位到要找的数据. ...

  7. Kryo官方文档-中文翻译

    Kryo作为一个优秀的Java序列化方案,在网上能找到不少测评,但未见系统的中文入门或说明文档.官方文档是最好的学习文档.虽然英文不差,但啃下来毕竟没母语来的舒服.这里抽出时间做些翻译,以方便大家查阅 ...

  8. 【JZOJ5086】【GDOI2017第四轮模拟day1】数列 折半搜索

    题面 有一个长度为n 的排列,现在有一些位置的数已经模糊不清了,你只知道这个排列的逆序对个数是K,你能计算出总共有多少可能的排列吗? 对于100% 的数据,n <=10^3,K<=10^9 ...

  9. Spring Boot自动配置原理(转)

    第3章 Spring Boot自动配置原理 3.1 SpringBoot的核心组件模块 首先,我们来简单统计一下SpringBoot核心工程的源码java文件数量: 我们cd到spring-boot- ...

  10. UWP获取任意网页加载完成后的HTML

    主要思想:通过后台WebView载入指定网页,再提取出WebView中的内容 关键代码: var html = await webView.InvokeScriptAsync("eval&q ...