Leetcode#73 Set Matrix Zeroes
用矩形的第一行和第一列充当mask
代码:
void setZeroes(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[].empty()) return; bool firstRow = false;
bool firstCol = false;
int m = matrix.size();
int n = matrix[].size(); for (int i = ; i < m; i++)
if (matrix[i][] == ) {
firstCol = true;
break;
} for (int j = ; j < n; j++)
if (matrix[][j] == ) {
firstRow = true;
break;
} for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (matrix[i][j] == ) {
matrix[i][] = ;
matrix[][j] = ;
}
}
} for (int i = ; i < m; i++)
if (matrix[i][] == )
for (int j = ; j < n; j++)
matrix[i][j] = ; for (int j = ; j < n; j++)
if (matrix[][j] == )
for (int i = ; i < m; i++)
matrix[i][j] = ; if (firstCol)
for (int i = ; i < m; i++)
matrix[i][] = ;
if (firstRow)
for (int j = ; j < n; j++)
matrix[][j] = ;
}
Leetcode#73 Set Matrix Zeroes的更多相关文章
- [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 ...
- [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. Follow ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- 【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】#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 ...
随机推荐
- php header 函数详解
网页的缓存是由 HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must- revalidate等,默认为private.其作用 ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- C# 代码重启windows服务
ServiceController service = new ServiceController("EnergyRecordService"); protected void b ...
- SQL语句的执行顺序
一.sql语句的执行顺序 (8)SELECT (9) DISTINCT (11) <TOP_specification> <select_list> (1) FROM < ...
- Oracle 直方图实例测试
--创建表 SQL> create table tab (a number, b number); Table created. --插入数据 SQL> begin .. loop ins ...
- [笔记]--Oracle修改SGA大小
1.通过spfile修改 (1)使用SYS用户以SYSDBA身份登录系统 (2)查看修改前sga_max_size,sga_target大小 show parameter sga_max_size; ...
- DeviceOne开发HelloWord
http://www.cnblogs.com/wjiaonianhua/p/5278061.html http://www.jb51.net/article/75693.htm 2015 年 9 月 ...
- 九度oj 1349 数字在排序数组中出现的次数
原题链接:http://ac.jobdu.com/problem.php?pid=1349 二分.. #include<algorithm> #include<iostream> ...
- iOS进阶学习-数据库
一.数据库管理系统 1.SQL语言概述:SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 2.常见 ...
- [quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming
[quote ]ffmpeg, gstreamer, Raspberry Pi, Windows Desktop streaming http://blog.pi3g.com/2013/08/ffmp ...