给定一个m x n的矩阵,如果某个元素为0,则把该元素所在行和列全部置0。

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

分析:最直接想到的就是遍历该矩阵,每遇到0就把它所在的行和列全部置0,但这是错的,因为这样会引入新的0到矩阵中。下一个比较容易相到的方法是:遍历矩阵,每遇到一个0元素就把它所在的行和列标记起来,最后再遍历matrix,若某元素的行或者列下标被标记,则置为0,这种方法实现方便,但是其空间复杂度为O(m+n)。如果先按行遍历,当遇到0时,就把该行的所有非0元素置为UINT_MAX;然后按列遍历,若遇到UNIT_MAX的元素就把其置为0,若遇到0元素就把整列置为0,这样就做到了O(1)的空间复杂度。下面把这两种方法的代码都贴上:

空间复杂度O(m+n)的方法:

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix)
{
int m = matrix.size();
if(0 == m) return;
int n = matrix[0].size(); int *rowFlags = new int[m]();
int *colFlags = new int[n](); for (int i=0; i<m; ++i)
{
for (int j=0; j<n; ++j)
{
if (matrix[i][j] == 0)
{
rowFlags[i] = 1;
colFlags[j] = 1;
}
}
} for (int i=0; i<m; ++i)
{
for (int j=0; j<n; ++j)
{
if (rowFlags[i] || colFlags[j])
matrix[i][j] = 0;
}
} delete [] rowFlags;
delete [] colFlags;
}
};

空间复杂度为O(1)的方法:

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix)
{
int m = matrix.size();
if(0 == m) return;
int n = matrix[0].size(); // 按行遍历,把包含0的行中的所有非0数用UINT_MAX标记
for (int i=0; i<m; ++i)
{
for (int j=0; j<n; ++j)
{
if (matrix[i][j] == 0)
{
for (j=0; j<n; ++j)
{
if (matrix[i][j] != 0)
matrix[i][j] = UINT_MAX;
}
break;
}
}
} // 按列遍历,把包含0的列中的所有数字置为0,并把UINT_MAX的元素置为0
for (int i=0; i<n; ++i)
{
for (int j=0; j<m; ++j)
{
if (matrix[j][i] == 0)
{
for (j=0; j<m; ++j)
matrix[j][i] = 0;
break;
} if(matrix[j][i] == UINT_MAX)
matrix[j][i] = 0;
}
}
}
};

【Leetcode】Set Matrix Zeroes的更多相关文章

  1. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  2. 【leetcode】Set Matrix Zeroes(middle)

    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】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  4. 【Leetcode】【Medium】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. 解题思路: ...

  5. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  6. 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  7. 【leetcode】Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  8. 【leetcode】Spiral Matrix

    题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...

  9. 【leetcode】Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

随机推荐

  1. vmware重装系统后虚拟机实例文件*.vmdk重用

    如题:vmware重装系统后自定义的:虚拟机名称*.vmdk文件重用. 一.问题描述 系统磁盘坏道,装不上系统直接换了硬盘,但是新装的Vmware不能够通过open方式打开“自定义*.vmdk”(这个 ...

  2. C读txt到二维数组

    #include<stdio.h> #include<stdlib.h> #define maxn 200 void main() { FILE *fp; int s[maxn ...

  3. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  4. phpadmin

    一晚上都在调试数据库,都要疯了,整理如下: 0.Apache服务器的443端口与VMware的冲突,所以要更改配置文件.设为440就可以(这个随意). 1.因为要远程访问,默认密码为空,所以首先给ro ...

  5. 聚类算法初探(四)K-means

    最近由于工作需要,对聚类算法做了一些相关的调研.现将搜集到的资料和自己对算法的一些理解整理如下,供大家参考. 另外在算法代码方面,我也做了一些实现(包括串行和并行),欢迎感兴趣的朋友探讨和交流. 第一 ...

  6. 解决的方法:warning: Clock skew detected. Your build may be incomplete.

    因为时钟同步问题.出现 warning:  Clock skew detected.  Your build may be incomplete.这种警告, 解决的方法: find . -type f ...

  7. OC中NSString 的常用方法

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...

  8. 吐槽下CSDN编辑器

    Perface 近期喜欢上了markdown,我认为它就是一些HTML标签的快捷键,用一些符号来取代标签,易学易读易用,何乐而不为呢?近期也喜欢用印象笔记来让我的记忆永存,确实它强大的收集能力让我迷上 ...

  9. oracle归档日志管理

    归档日志(Archive Log)是非活动的重做日志备份.通过使用归档日志,可以保留所有重做历史记录,当数据库处于ARCHIVELOG模式并进行日志切换式,后台进程ARCH会将重做日志的内容保存到归档 ...

  10. "margin塌陷现象"div盒子嵌套盒子外边距合并现象

    问题描述:原型大概是“一个div嵌套了两个 div,给main设定了background="pink" ,header1设定background=“red” .header2 设定 ...