【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 ...
随机推荐
- Map map=new HashMap(); 为什么是这样
Map是接口,hashMap是Map的一种实现.接口不能被实例化. Map map=new HashMap(); 就是将map实例化成一个hashMap.这样做的好处是调用者不需要知道map具体的实现 ...
- cf455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- GIS 相关知识扫盲
1.什么是GIS GIS:地理信息系统,它是一种特定的十分重要的空间信息系统.它是在计算机硬.软件系统支持下,对整个或部分地球表层(包括大气层)空间中的有关地理分布数据进行采集.储存.管理.运算.分析 ...
- block的一点知识
一个block的笔记: http://www.cnblogs.com/xinye/archive/2013/03/03/2941203.html http://segmentfault.com/q/1 ...
- MS SQL 当记录不存在时插入insert INTO not exists
INSERT INTO dbo.[T_DabaoTemp] ([PType] ,[pID] ,[NewVersion] ,[ParentC ...
- 如何使用Storyboard创建UIPageViewController
之前我们已经讲过UIPageViewController,那篇文章演示了如何使用Interface Builder创建UIPageViewController.为了适配iOS7和Xcode5,我们重新 ...
- Ubuntu下nginx+uwsgi+flask的执行环境搭建
选择web framwork是个非常艰难的事情, 主要分为轻量级和重量级框架. 因为没有搭建站点这样的须要, 所以回避SSH, Django这样的框架, 而选择一个轻量级框架. 自己也比較青睐pyth ...
- linux下的DNS
Linux下设置DNS的位置主要是, 1网卡设置配置文件里面DNS服务器地址设置;2 hosts文件指定 3.系统默认DNS服务器地址设置/etc/resolv.conf文件修改 生效顺序是: 1 h ...
- Hexo 官方主题 landscape-plus 优化
博主喜欢简洁大方的Hexo主题,看了不下100个主题之后,最终选择了 landscape-plus 主题(针对中国大陆地区,对Hexo官方主题landscape进行优化后的版本).更多Hexo主题资源 ...
- Array数组方法的总结
添加元素: 1. push(arg1,arg2,arg3....) 向数组的尾部添加元素,返回值是数组的长度. 2.unshift(arg1,arg2,arg3....) 向数组的头部添加元素,返回 ...