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. HDU1814 2-sat 模板

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. python读书笔记-django架站过程总结(from the django book)

    django架站过程总结:1.django-admin startproject store2.store这个project的目录下有:__init__,manage,setting,urls3.se ...

  3. oracle to_char格式数值

    C:\Users\XXX>sqlplus / as sysdba SQL :: Copyright (c) , , Oracle. All Rights Reserved. 连接到: Oracl ...

  4. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  5. Ubuntu12.04 安装nginx和mongo过程

    1.安装php和php-cgi apt-get install php5 php5-cgi 2.安装 nginx apt-get install nginx 3.安装 MongoDB apt-get ...

  6. [uva11997]k个最小和

    一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和. k<=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i<=k)+a[ ...

  7. 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial

    [题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...

  8. 【BZOJ】1699 [Usaco2007 Jan]Balanced Lineup排队

    [算法]线段树 #include<cstdio> #include<cctype> #include<algorithm> using namespace std; ...

  9. 有关计数问题的dp

    问题一:划分数 问题描述 有n个去区别的物体,将它们划分成不超过m组,求出划分方法数模M的余数. 我们定义dp[i][j],表示j的i划分的总数 将j划分成i个的话,可以先取出k个,然后将剩下的j-k ...

  10. 项目记录 -- zpool set

    zfs set <property=value> <filesystem|volume|snapshot> root@UA4300D-spa:~/hanhuakai/pro_0 ...