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——常数空间内完成的更多相关文章

  1. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

  2. 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. 很挫的一个想 ...

  3. 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 ...

  4. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  5. 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 ...

  6. [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 ...

  7. 【数组】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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. JavaScript中进制之间的转换

    JavaScript中进制之间的转换 //十进制转其他 var x = 100; alert(x); alert(x.toString(2)); //转2进制 alert(x.toString(8)) ...

  2. Codeforces Round #345 (Div. 2) B

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. oracle 国外网站【转载】

    [转自]:http://www.2cto.com/database/201406/306615.html 1. http://www.oratechinfo.co.uk/ http://www.ora ...

  4. maven中net.sf.json报错的解决方法(转载)

    原文:http://www.cnblogs.com/winner-0715/p/5928514.html 今天在用maven添加net.sf.json的jar包的时候,代码如下: <depend ...

  5. 使用kubeadm安装Kubernetes 1.12

    使用kubeadm安装Kubernetes 1.12 https://blog.frognew.com/2018/10/kubeadm-install-kubernetes-1.12.html 测试环 ...

  6. [LeetCode] 16. 3Sum Closest ☆☆☆

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. java 7修改文件权限

    Full control over file attributes is available in Java 7, as part of the "new" New IO faci ...

  8. 【BZOJ】1014 [JSOI2008]火星人prefix

    [算法]splay [题解]对于每个结点维护其子树串的hash值,前面为高位,后面为低位. sum[x]=sum[L]*base[s[R]+1]+A[x]*base[s[R]]+sum[R],其中su ...

  9. python学习笔记(六)之操作符

    python中算术操作符: + - * / % ** // 注意: /:为真实除法,即对应数学中的除法,通常返回一个浮点数 //:取整除法,即取商 %:求模,即取余数 **:幂运算,这里需要注意的一点 ...

  10. 01背包问题的延伸即变形 (dp)

    对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...