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 ...
随机推荐
- table中绝对定位元素相对td定位失效解决方案
开门见山! 问题:在一个table中,我需要在td里面绝对定位一个div, 写法:td{position:relative;} div{position:absolute;} OK,就这么简单,思路也 ...
- Mysql查询(笔记二)
1.两结构相同的表数据间移植 Inset into 表一 Select 字段1,字段2,....字段n from表二 建立数据库时设置数据库编码 create database 数据库名 charse ...
- php常用函数集锦[备份用的]
1.判断是否正确的日期格式 /** * 是否正确的日期 * * @access public */ private function _isdate($str,$format="Y-m-d ...
- 多层级Spinner列表选项实时更新树形层级(选择城市)
package com.example.spinnerdemo; import android.os.Bundle; import android.app.Activity; import andro ...
- ldd3-2 构造和运行模块:环境搭建2
之前搭建了Ubuntu10.04驱动开发环境,但是那儿的内核版本是2.6.32.27,总感觉无从下手的感觉,因为书上的内核版本是2.6.10,作为初学者不知道差异在哪里,或许不应该纠结这个问题吧. 昨 ...
- ios 异步处理耗时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_asy ...
- 成功完成Moses Manual中BaseLineSystem
终于把这个破法语句子翻译出来了,各种耗时,bug,弄了一天,明天争取看完详细的翻译教程! 而且还要学习中文分词,晚安,Moses!
- MongoDB仲裁节点的理解以及memcached,zookeeper,redis,故障恢复方案思考.
在进行副本集部署时我们会添加一个或多个仲裁节点,仲裁节点不用于备份数据,由于它职责的职责是负责选举主节点,所以对硬件没有太高要求,可以将它部署在单独的服务器上,这个服务器可以是监听服务器,也可以部署在 ...
- spring使用JdbcDaoSupport中封装的JdbcTemplate进行query
1.Dept package cn.hxex.springcore.jdbc; public class Dept { private Integer deptNo; private String d ...
- XAML特殊字符
此部分只限制在XAML中,代码中不受此类限制. 1.特殊字符转义 XAML 特殊字符转义 特殊字符 转义 小于号 < < 大于号 > > 取址符 & & 引号 ...