【Leetcode】Set Matrix Zeroes
给定一个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的更多相关文章
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- 【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. 思路:不能用 ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【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. 解题思路: ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
- 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【leetcode】Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- 【leetcode】Spiral Matrix
题目概要: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spi ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
随机推荐
- linux之SQL语句简明教程---WHERE
我们并不一定每一次都要将表格内的资料都完全抓出.在许多时候,我们会需要选择性地抓资料.就我们的例子来说,我们可能只要抓出营业额超过 $1,000 的资料.要做到这一点,我们就需要用到 WHERE 这个 ...
- windows Oracle DBases auto backUp
- ListView嵌套ListView时发生:View too large to fit into drawing cache的问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXkxMzg3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- 月赛-Crackhash
Crackhash 这个题目是我为月赛出的,完全仿照自mma 1st simple_hash. 这道题目比较有意思的地方在于在32位的程序中模拟了64位的算术运算. 题目的思路很清晰.要求输入全为数字 ...
- 【MFC初学】
void CMy322Dlg::OnButton1() { UpdateData(TRUE); m_crypt=m_plaintxt; for(int i=0;i<m_plaintxt.GetL ...
- C# 中的委托和事件详解
C# 中的委托和事件 文中代码在VS2005下通过,由于VS2003(.Net Framework 1.1)不支持隐式的委托变量,所以如果在一个接受委托类型的位置直接赋予方法名,在VS2003下会报错 ...
- SQL中数据类型转换
CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...
- C# VS 面向对象基础(一)
面向对象(Object Oriented,OO)的相关知识学习了很多了,这篇博客我从C#实现面向对象的理论来做个初步的总结. 在这篇博客中,我通过一个例子讲述了,面向对象中,类,类的实例化,构造方法, ...
- sql生成20位数随机数
declare @rnd nvarchar(50)set @rnd =''while LEN(@rnd)<20 begin set @rnd =@rnd + REPLACE ( CONVERT( ...
- 【noip2012提高组】国王游戏
恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 成一排,国王站在队伍的最前面. ...