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 ...
随机推荐
- 1238. Folding
http://acm.timus.ru/problem.aspx?space=1&num=1238 DP+记忆化搜索 思路不难,关键是最优结果的储存问题,为了编写方便,直接用string储存最 ...
- 作业七:团队项目——Alpha版本冲刺阶段 001
今天进展:准备开发环境,安装软件. 今天安排:因为软件过于庞大,所以我们第一天都在按软件,原本计划第一天要设计框架,但因为软件问题.所以我们决定留到第二天.
- FlashBuilder使用
打开调用层次视图,显示当前类.变量被谁调用,右侧显示调用位置. ctrl+alt+H 快捷键 导航即浏览菜单中,单击. 右键单击打开. 为组件生成事件处理函数 组件==控件,按钮等.右侧属性,又叫属性 ...
- Maven工程中的右键team
与资源库同步(S):在需要合并版本时使用 提交(C):本地代码写入源码库 更新(U):本地代码升级到服务器端版本 在点击更新时,请注意: 如果当前项目有改动(甚至是比原来多了一个空格),则此时无法更新 ...
- KVC/KVO原理详解及编程指南
一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...
- MYSQL基本操作语句
0.修改密码:mysqladmin -u root -p password 123456 导出数据库:mysqldump -u root -p yunpay>yunpay.sql 导入数据库:m ...
- |原创|unity 4.3 2D功能SpriteRenderer修改颜色的方法
4.3增加了不少2D功能,然后实在没有找到有人分享,在国外查资料研究一下午然后给个简单的教程 ===================================================== ...
- CE 文件读写操作
写入字符到文件中: // TODO: 写字符到文件 // 参数: CString类型的文件名FileName;char *类型的数据内容;unsigned int类型内容长度 // 返回: 成功返回T ...
- DWORD类型的IP地址转换为CString字符串
从ip地址控件获得的ip地址是DWORD类型的 用MessageBox怎样将ip地址显示出来呢? DWORD类型32位,每4位为一组代表常见的IP地址,即***.***.***.***. 采用HIWO ...
- 认识CPU Cache
http://geek.csdn.net/news/detail/114619 7个示例科普CPU Cache:http://coolshell.cn/articles/10249.html Linu ...