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 ...
随机推荐
- 【题解】CF375D Tree and Queries
Link \(\text{Solution:}\) 讲实话这题有点烦,不知道为啥改了下\(\text{dfs}\)就过了--原版本\(dfs\)好像没啥错啊-- 其实对于子树问题,我们求出原来树的\( ...
- IDEA使用正则表达式替换
替换目标:为value添加函数『JSON.stringify()』 vars.put("_id",value); 表达式: //find: (vars.put\(\"_i ...
- 正则表达式查找“不包含XXX字符串”
使用 当我要找到不包含某些字符串(如test)时, 可以使用 # 独立使用 (?!test). # 加头尾判断 ^((?!test).)*$ 原理 正则表达式的断言功能: (?=pattern) 非获 ...
- 上帝视角一文理解JavaScript原型和原型链
本文呆鹅原创,原文地址:https://juejin.im/user/307518987058686/posts 前言 本文将从上帝角度讲解JS的世界,在这个过程中,大家就能完全理解JS的原型和原型链 ...
- vue获取下拉框select的值
1.我写的是循环遍历,然后获取id :value="v.id"这就是获取的id然后打印就可以获取id了
- devops-jenkins分布式构建
1. devops-jenkins分布式构建 1) 点击系统管理 进入节点管理 2) 点击新建节点 3) 进行节点配置在node节点服务器配置,进行java的安装 [root@nginx-backup ...
- Python基础笔记2-ruamel.yaml读写yaml文件
上一篇笔记记录了Python中的pyyaml库对yaml文件进行读写,但了解到ruamel.yaml也能对yaml文件进行读写,于是想尝试一下它的用法. 一,注意 这里首先要更正一下网上大部分博客的说 ...
- 多测师讲解接口测试 _fiddler无法打开浏览器_高级讲师肖sir
错误现象: 打开fi'd'dler以后浏览器无法打开,关闭fiddler之后可以打开浏览器 解决方案: 1. 2. 在点击确定
- 【不知道怎么分类】CF 819B Mister B and PR Shifts
题目内容 洛谷链接 定义一个全排列\(p_i\)的偏移值为\(\sum_{i=1}^{n}|p[i]-i|\). 给你一个全排列,你可以从后面拿\(k\in[0,n-1]\)个数放在前面,使得该全排列 ...
- ImageMagick实现图片加水印(ImageMagick6.9.10)
一,ImageMagick的安装 请参见: https://www.cnblogs.com/architectforest/p/12807514.html 说明:刘宏缔的架构森林是一个专注架构的博客, ...