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 ...
随机推荐
- hadoop拷贝文件时 org.apache.hadoop.ipc.RemoteException异常的解决
1.系统或hdfs是否有空间 2.datanode数是否正常 3.是否在safemode 4.防火墙关闭 5.配置方面 6.把NameNode的tmp文件清空,然后重新格式化NameNode
- 微软推荐的130道ASP.NET常见面试题及答案
1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问. protected : 保护成 ...
- .net面试题大全(有答案)
在网上找来的,希望对大家有所帮助. 1 (1)面向对象的语言具有__继承性_性._封装性_性._多态性 性. (2)能用foreach遍历访问的对象需要实现 _ IEnumerable 接口或 ...
- HDU 4868 Information Extraction(2014 多校联合第一场 H)
看到这道题时我的内心是奔溃的,没有了解过HTML,只能靠窝的渣渣英语一点一点翻译啊TT. Information Extraction 题意:(纯手工翻译,有些用词可能在html中不是一样的,还多包涵 ...
- 数据字典的QUAN DEC类型与ABAP P型转换
转至:http://sap.iteye.com/blog/121584 今天突然想到的,肯定很多人知道,但是也肯定有一大堆人不知道. 转换公式 (n+1)/2 比如DEC定义为13位,其中3位小数 ...
- Net 并行知识学习
园子里有很多介绍并行库TPL的这里列举2个 1 并行之Task讲解:http://www.cnblogs.com/yanyangtian/archive/2010/05/21/1741193.html ...
- Nginx设置alias实现虚拟目录 alias与root的用法区别
Nginx 貌似没有虚拟目录的说法,因为它本来就是完完全全根据目录来设计并工作的.如果非要给nginx安上一个虚拟目录的说法,那就只有alias标签比较"像",干脆来说说alias ...
- 简单实用的日志类CLog (Python版)
#coding: utf-8 import time ''' /***************************************************************** Fu ...
- URAL 1056(树形DP)
1056. Computer Net Time limit: 2.0 second Memory limit: 64 MB Background Computer net is created by ...
- information_schema模式表介绍 processlist
在mysql里,我们一般通过show (full)processlist查看当前连接情况,处理各种数据库问题.现在在information_schema模式下,5.5以后增加了processlist表 ...