【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 ...
随机推荐
- 纯CSS美化的checkbox 和 radio
html <!DOCTYPE HTML> <html> <head> <title>纯CSS3实现自定义美化复选框和单选框</title> ...
- mariadb启动
systemctl start mariadb.service #启动MariaDBsystemctl stop mariadb.service #停止MariaDBsystemctl restart ...
- cf466C Number of Ways
C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 常用的IO流
常用的IO流 •根据处理数据类型的不同分为:字节流和字符流 •根据数据流向不同分为:输入流和输出流 字节流:字节流以字节(8bit)为单位,能处理所有类型的数据(如图片.avi等). 字节输入流:In ...
- 蓝桥杯之JAM的计数法
题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...
- ubuntu设置系统时间与网络时间同步和时区
Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...
- RMAN备份之非归档模式下的备份
Backing Up a Database in NOARCHIVELOG Mode:1.Log into RMAN2.Shutdown immediate from RMAN3.Startup mo ...
- Devexpress之barManager控件属性
隐藏菜单栏左边的竖线和右边的箭头? 1.隐藏菜单栏上右边的箭头属性设置:OptionsBar=>>AllowQuickCustomization=False 2.隐藏菜单栏左边的竖线属性设 ...
- js 刷新网页
1. Javascript 返回上一页history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). 3. window.history.forw ...
- 必须知道的ADO.NET 数据库连接池
http://www.cnblogs.com/liuhaorain/archive/2012/02/19/2353110.html 题外话 通过前几章的学习,不知道大家对ADO.NET有一定的了解了没 ...