Leetcode 细节实现 Set Matrix Zeroes
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.
题意:给定矩阵,假设矩阵的某个位置为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的更多相关文章
- 【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 ...
- leetcode第一刷_Set Matrix Zeroes
这个题乍一看非常easy,实际上还挺有技巧的.我最開始的想法是找一个特殊值标记.遇到一个0,把他所相应的行列中非零的元素标记成这个特殊值.0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】-- 73. Set Matrix Zeroes
问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
- 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 ...
- 【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 ...
- 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. 这题要注意的 ...
- 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 ...
随机推荐
- linux下nginx负载均衡部署
nginx负载均衡部署 Nginx("engine x") 是一个高性能的 HTTP 和 反向代理 server,也是一个 IMAP/POP3/SMTP 代理server. Ngi ...
- C++基础学习笔记----第七课(面向对象的基本概念)
主要讲面向对象的基本概念和一些概念,以及实现简单的面向对象C++程序. 类和对象 基本概念 类和对象是面向对象中的两个基本概念,类是指一类事物,是一个抽象的概念.对象是指某一个类的实体,是一个具体存在 ...
- 【Demo 0008】Java基础-抽象类
本掌学习要点: 1. 了解抽象类的定义及使用场景: 2. 掌握final修饰的类.方法以及变量作用及用法: 3. 掌握abstract 修饰的类及方法作用及用 ...
- [置顶] Java字节码文件剖析
Java为什么能够支持跨平台,其实关键就是在于其*.class字节码文件,因为*.class字节码文件有一个统一标准的规范,里面是JVM运行的时需要的相关指令,各家的JVM必须能够解释编译执行标准字节 ...
- HDU 4344 随机法判素数(费马小定理
#include <cstdio> #include <ctime> #include <cmath> #include <algorithm> usi ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Network Panel说明
Chrome Developer Tools:Network Panel说明 官方资料:Chrome Developer Tools: Network Panel 一.chrome Develop ...
- CImageList使用简要说明
CImageList ImageList;//创建一个包含3个24位色32x32图片的ImageList,ILC_MASK的意思是同时创建一个mask,这样在下面指定了背景颜色以后ImageList就 ...
- EXTJS4两个ComboBox的数据源联动,解决遇到第二个ComboBox第二次以后显示忙的状态问题
定义如下[红色部分是后加上的,它是解决问题的关键]: var bu_store = Ext.create('Ext.data.Store', { fields: ['key', 'value'], r ...
- 如何隐藏 QLPreviewController 的 Action 按钮?
在 iOS 6 以前,可以在 present QLPreviewController 之后使用以下代码: [previewController.navigationItem setRightBarBu ...