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. bzoj2761: [JLOI2011]不重复数字(hash)

    题目大意:给出N个数,要求把其中重复的去掉,只保留第一次出现的数.例如,给出的数为1 2 18 3 3 19 2 3 6 5 4,其中2和3有重复,去除后的结果为1 2 18 3 19 6 5 4. ...

  2. Python图像处理库(PIL)

    官方:(详细)http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html http://pillow.readthedocs.io/e ...

  3. mysql 常用总结

    centos7 安装mysql 数据库安装参考:http://www.cnblogs.com/longrui/p/6071581.htmlhttps://www.cnblogs.com/yoursou ...

  4. zoj 2369 Two Cylinders

    zoj 2369 Two Cylinders 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2369 题意:已知两个无 ...

  5. 寻找最大连续子序列/Find the max contiguous subsequence

    寻找最大连续子序列 给定一个实数序列X1,X2,...Xn(不需要是正数),寻找一个(连续的)子序列Xi,Xi+1,...Xj,使得其数值之和在所有的连续子序列数值之和中为最大. 一般称这个子序列为最 ...

  6. LightOJ 1009 二分图染色+BFS/种类并查集

    题意:有两个阵营的人,他们互相敌对,给出互相敌对的人,问同个阵营的人最多有多少个. 思路:可以使用种类并查集写.也可以使用使用二分图染色的写法,由于给定的点并不是连续的,所以排序离散化一下,再进行BF ...

  7. [Luogu 3973] TJOI2015 线性代数

    [Luogu 3973] TJOI2015 线性代数 这竟然是一道最小割模型. 据说是最大权闭合子图. 先把矩阵式子推出来. 然后,套路建模就好. #include <algorithm> ...

  8. 调戏OpenShift:一个免费能干的云平台(已失效)

    一.前因后果 以前为了搞微信的公众号,在新浪sae那里申请了一个服务器,一开始还挺好的 ,有免费的云豆送,但是一直运行应用也要消费云豆,搞得云豆也所剩无几了.作为一名屌丝,日常吃土,就单纯想玩一玩微信 ...

  9. uva10766生成树计数(矩阵树定理)

    更正了我之前打错的地方,有边的话G[i][j]=-1; WA了好多次,中间要转成long double才行..这个晚点更新. #include<cstdio> #include<cs ...

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

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