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?
1,O(mn),即用的一个相同大小的矩阵来记录那个位置有0.
2,O(m + n),就是加一行一列来标记哪行哪列有0。
3,常数空间,即考虑不使用额外的空间,可以把第一行与第一列作为标记行与标记列,但是得先确定第一行与第一列本身要不要设为0。
代码非常简单:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size() < ) return;
int row = matrix.size(), col = matrix[].size();
bool r0 = false, c0 = false;
for (int i = ; i < row; ++i) {
if (matrix[i][] == ) {
c0 = true; break;
}
}
for (int j = ; j < col; ++j) {
if (matrix[][j] == ) {
r0 = true; break;
}
}
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
matrix[i][] = (matrix[i][j] == ) ? : matrix[i][];
matrix[][j] = (matrix[i][j] == ) ? : matrix[][j];
}
}
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
matrix[i][j] = (matrix[i][] == ) ? : matrix[i][j];
matrix[i][j] = (matrix[][j] == ) ? : matrix[i][j];
}
}
for (int i = ; i < row && c0; ++i) matrix[i][] = ;
for (int j = ; j < col && r0; ++j) matrix[][j] = ;
}
};
Set Matrix Zeroes——常数空间内完成的更多相关文章
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
- 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. 很挫的一个想 ...
- leetcode_question_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 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- 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 ...
- [Swift]LeetCode73. 矩阵置零 | 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 ...
- 【数组】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. cl ...
- 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解题报告—— 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 ...
随机推荐
- Django Session配置
Django Session的三种存储方式 SESSION_ENGINE='django.contrib.sessions.backends.db' # default 保存到数据库中,依赖 'dja ...
- JQuery选择符的理解与应用
JQuery强大的选择符可以让我们获得页面中任何元素进行操作,并且使用简单方便,可读性强.本章内容根据本人在开发中常用到的选择符作为例子来进行讲解,如有更多常用的简单的例子可回复提供,参与讨论,一起学 ...
- UIColor延伸:判断两个颜色是否相等
不管UIColor使用CIColor,CGColor还是其他方式初始化的,其CGColor属性都是可用的.CoreGraphics中提供一个函数,用于判断两个CGColor是否相等,因此我们可以通过这 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- bzoj 4402 Claris的剑 组合数学
Claris的剑 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 375 Solved: 213[Submit][Status][Discuss] D ...
- C# 把类实例保存到文件里(类的序列化和反序列化)
有时候我们希望把类的实例保存下来,以便以后的时候用.一个直观的方法就是StreamWriter把类写成一行,用\t分隔开每个属性,然后用StreamReader读出来. 但是这样太麻烦,代码行数较多, ...
- centos6.5 配置mongodb3
下载地址 http://www.mongodb.org/downloads 下载 curl -O -L https://fastdl.mongodb.org/linux/mongodb-linux-i ...
- svg学习
百度百科: SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C制定的一种新的二维矢量图形格式,也 ...
- [LeetCode] 2. Add Two Numbers ☆☆
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- 如何生成Java Key以及sign一个jar
1. 生成Java Key: keytool -genkey -alias mydomain -keyalg RSA -keystore keystore.jks -keysize mydomain ...