leetcode76set-matrix-zeroes
题目描述
Follow up: Did you use extra space?
A straight forward solution using O(m n) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
const int row=matrix.size();
const int col=matrix[0].size();
bool row_flag=false,col_flag=false;
for (int i=0;i<row ;i++)
if (0==matrix [i][0])
{
col_flag=true;
break;
}
for (int i=0;i<col;i++)
if (0==matrix[0][i])
{
row_flag=true;
break;
}
for (int i=1;i<row;i++)
for (int j=1;j<col;j++)
if (0==matrix[i][j]){
matrix[i][0]=0;
matrix[0][j]=0;
}
for (int i=1;i<row;i++){
for (int j=1;j<col;j++){
if (0==matrix[i][0] || matrix [0][j]==0)
matrix[i][j]=0;
}
}
if (row_flag)
for (int i=0;i<col;i++)
matrix[0][i]=0;
if (col_flag)
for (int i=0;i<row;i++)
matrix[i][0]=0;
}
};
leetcode76set-matrix-zeroes的更多相关文章
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- Leetcode 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- LeetCode: Set Matrix Zeroes 解题报告
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...
- 【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解题报告—— 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 ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- Set Matrix Zeroes leetcode java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fol ...
- [LeetCode] 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 ...
随机推荐
- P5664 Emiya 家今天的饭
题面 link 前言 去年把我做自闭的一道题,看了一眼题面,发现只有 t1 有点思路,结果写到一半发现自己读错题了,又只能花时间来重构,结果后面的暴力一点都没写(主要是自己当时不会) 然后,这道题还因 ...
- Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二)
Spring Boot+Spring Security+JWT 实现 RESTful Api 认证(二) 摘要 上一篇https://javaymw.com/post/59我们已经实现了基本的登录和t ...
- day65:nginx代理&nginx负载均衡
目录 1.nginx代理 2.nginx代理与配置 3.nginx负载均衡调度多web节点(静态页面) 4.nginx负载均衡调度多应用节点(blog) 5.nginx_proxy + web应用节点 ...
- 浅谈Python常用英文单词
一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7.charac ...
- 五分钟详解MySQL并发控制及事务原理
在如今互联网业务中使用范围最广的数据库无疑还是关系型数据库MySQL,之所以用"还是"这个词,是因为最近几年国内数据库领域也取得了一些长足进步,例如以TIDB.OceanBase等 ...
- .NET Core开源任务调度平台ScheduleMaster上新了
ScheduleMaster上一次比较大的更新还是在6月份,转眼已经快过去4个月了,这段时间比较忙,中间只更新过一次修复了几个小bug.要总结这次更新的话,必须要用"千呼万唤始出来" ...
- Linux命令获得帮助
在Linux中获得帮助 查帮助的思路 whatis CMD mandb type CMD 如果内部:help CMD ; man bash 如果外部:CMD --help | -h 概述 获取帮助的能 ...
- 为C量身定制的Matrix库
Matrix库的诞生让矩阵操作在C中也可以玩的很溜! 项目地址:https://github.com/SJ2050SJ/Matrix 文章目录 Matrix的设计框架 Matrix的上手简历 Matr ...
- LruCache缓存bitmap(二)
Lrucache缓存程序关闭缓存自动清除,所以要在onstart方法中调用,只要不关闭程序缓存就在,除以1024是以kb为单位 public class MainActivity extends Ap ...
- Hive sql函数
date: 2018-11-16 19:03:08 updated: 2018-11-16 19:03:08 Hive sql函数 一.关系运算 等值比较: = select 1 from dual ...