55. Set Matrix Zeroes
Set Matrix Zeroes
(Link: https://oj.leetcode.com/problems/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.
Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution?
思路: 找到第一个 0 元素,记下其行和列。然后对其他 0 元素,分别将其投影在记下的行和列上(做标记)。遍历之后,对于所有行中的标记,将其所在列都置为 0; 对于所有列中标记,将其所在行都置为 0. (最后置标记的行和列为 0. 可含在上述步骤) 时间: O(n2), 空间 : O(1)
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(!matrix.size() || !matrix[0].size()) return;
const int INF = 1001;
int row = matrix.size(), col = matrix[0].size();
int u = -1, v = -1;
for(int r = 0; r < row; ++r)
for(int c = 0; c < col; ++c) {
if(matrix[r][c] == 0) {
if(u == -1) { u = r, v = c; continue; }
matrix[u][c] = INF;
matrix[r][v] = INF;
}
}
if(u == -1) return;
if(matrix[u][v] == INF) matrix[u][v] = 0;
for(int c = 0; c < col; ++c) {
if(matrix[u][c] == INF) {
for(int i = 0; i < row; ++i) matrix[i][c] = 0;
} else matrix[u][c] = 0;
}
for(int r = 0; r < row; ++r) {
if(matrix[r][v] == INF) {
for(int j = 0; j < col; ++j) matrix[r][j] = 0;
} else matrix[r][v] = 0;
}
}
};
55. Set Matrix Zeroes的更多相关文章
- [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 ...
随机推荐
- PHP 每天的总结(1)
今天写博客的心情比以往还要糟糕,因为........................(完结). 1.获取某输入框的值,有两中获取方式,GET和POST .前者 没有为顾客的账户安全着想.而后者返之: ...
- Servlet里面url-pattern的通配符*的使用规则
简单来说: 以”/’开头和以”/*”结尾的是用来做路径映射的. 以前缀”*.”开头的是用来做扩展映射的. “/” 是用来定义default servlet映射的. 剩下的都是用来定义详细映射的.比如: ...
- 例子:Alarm Clock with voice Commands Sample
通过本例子学习: 如何使用自定义字体文件(.TTF) 如何播放声音 动画的使用 Speech 设置闹铃 应用 设置 数据存储到IsolatedStorage 如何使用自定义字体文件(.TTF) < ...
- Android应用增加计时器
昨天写的Sudoku游戏需要增加计时器功能,使用Chronometer实现如下,由于Chronometer自己在调用stop之后后台的计时器还会继续增加,所以暂停功能需要额外实现: 在StartAct ...
- C++嵌入Python,以及两者混用
以前项目中是C++嵌入Python,开发起来很便利,逻辑业务可以放到python中进行开发,容易修改,以及功能扩展.不过自己没有详细的研究过C++嵌入python的细节,这次详细的研究一下.首先我们简 ...
- Java-->Gson解析相较于Json
--> Gson解析jar包: 链接:http://pan.baidu.com/s/1slCeq77 密码:f9ig --> 官方Json解析工具类: package com.drago ...
- 第一次装TFS的曲折经历
公司服务器之前TFS没装上,用的svn,实在忍受不了了,作为一个.net程序员怎么能用svn的,说动手就动手. 服务器的数据库是 SqlServer2008R2 本来想装TFS2013,后来试了一下, ...
- 关于wcf异常异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribut
异常信息:由于内部错误,服务器无法处理该请求.有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribut ...
- 正向代理与反向代理的区别【Nginx读书笔记】
正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板, 简单的说, 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 这个代理服务器呢,他能访问那个我不能访问的网站 于是我 ...
- [HNOI2008],[bzoj1008] 越狱(dp+组合数学)
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1008 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人 ...