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. Golang命名规范和开发规范

    目录 命名 文件命名 package 变量 常量 接口 结构体 方法 注释 README 命名 文件命名 文件命名一律采用小写,不用驼峰式,尽量见名思义,看见文件名就可以知道这个文件下的大概内容. 其 ...

  2. Leetcode 485. 最大连续1的个数

    1.题目描述(简单题) 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 ...

  3. HRBUST 1819

    石子合并问题--圆形版 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 61(27 users) Total Accepted: 26( ...

  4. Codeforces Round #407 (Div. 2) D,E

    图论 D. Weird journey time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  6. ZooKeeper食谱(八)

    使用ZooKeeper构造高级别应用的指南 在这个文章中,你将会发现使用ZooKeeper来实现高级别功能的指南.所有的它们在客户端上被实现而不需要ZooKeeper特别的支持.希望社区将注意到这些约 ...

  7. CentOS安装JDK环境

    一:查看当前系统的java环境 [elsearch@localhost data]$ rpm -qa | grep jdk 二:卸载原有的jdk [elsearch@localhost /]$ yum ...

  8. 【hdu3033】分组背包(每组最少选一个)

    [题意] 有S款运动鞋,一个n件,总钱数为m,求不超过总钱数且每款鞋子至少买一双的情况下,使价值最大.如果有一款买不到,就输出“Impossible". 1<=N<=100  1 ...

  9. charles https抓包

    1. 配置 Charles 根证书 首先打开 Charles: Charles 启动界面 主界面 然后如下图操作:   之后会弹出钥匙串,如果不弹出,请自行打开钥匙串,如下图: 钥匙串 系统默认是不信 ...

  10. new操作符(翻译自mozilla.org)

    翻译自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new new操作符可以实例化一个用户自 ...