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. UVA 644 Immediate Decodability (字符处理)

    An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the ...

  2. Android中Activity之间访问互传参数

    public class MainActivity extends Activity { private static final int OTHER = 1; @Override protected ...

  3. <转载>CSS解决图片过大撑破DIV的方法

    DIV+CSS网页内容中如果插入大于DIV层宽度显示,过大的图片将会撑破网页宽度显示从而网页严重变形,您是否遇到过?这里DIVCSS5给大家介绍几种解决图片撑破撑开网页DIV层方法. 图片撑破宽度解决 ...

  4. Selenium Webdriver ie 浏览器

    webDriver 在测试ie 的时候会遇到很多的问题,记录下: 1.需要ie的driver驱动 需要下载 IEDriverServer.exe 并把这个驱动放在系统ie 的文件夹下 C:\Progr ...

  5. 基于Spring提供支持不同设备的页面

    基于Spring来检测访问Web页面的设备是很简单的,在这个经验中我们讲到过.通常不同的设备访问我们是通过响应式设计来统一处理各种设备的尺寸的.但是如果希望针对不同的设备,显示不同的内容呢? Spri ...

  6. JavaScript快速入门(五)——表达式运算

    赋值运算 赋值运算的形式为左值 = 右值.如果同个表达式中有多个赋值运算,则从右到左运算.例如: a = b = c; // 和下面两行等价 b = c; a = b; 另外一种赋值运算的形式叫做复合 ...

  7. Lucene.Net 2.3.1开发介绍 —— 四、搜索(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(二) 4.3 表达式用户搜索,只会输入一个或几个词,也可能是一句话.输入的语句是如何变成搜索条件的上一篇已经略有提及. 4.3.1 观察 ...

  8. SDL 简介

    SDL 简介 什么是SDL? 即 Simple DirectMedia Layer,使用 LGPL 许可证. 免费的跨平台多媒体应用编程接口 用于游戏.游戏开发工具.模拟器.样本演示.多媒体应用等 它 ...

  9. OpenRisc-30-SD card controller模块分析与验证

    引言 ORPSoC的硬件平台是包含SD card controller控制器的,但是对应的linux里面却没有对应的linux的驱动程序,这使ORPSoC的SD card的使用收到了很大的限制.没有驱 ...

  10. 设定十分钟android在状态栏上集成的开源project推荐

    1.前言 于android kitkat 有一个新功能可以设置背景的手机状态栏,让手机风的整个界面格是一致的,它看起来非常酷,在今年的google i/o向上android l这样的风格.来如今看我们 ...