题目

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

click to show follow up.

Follow up:

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?

分析

题目给定一个m∗n矩阵,要求将矩阵中值为0的元素所在的整行、整列元素均赋值为0;

重点是要求算法是本地,也就是说空间复杂度为O(1)

具体思路如下:

利用矩阵的第一行和第一列来作为辅助空间使用,不用开辟新的存储空间。

方法就是:

1.先确定第一行和第一列是否需要清零

即,看看第一行中是否有0,记下来;也同时记下来第一列中有没有0。

2.扫描剩下的矩阵元素,如果遇到了0,就将对应的第一行和第一列上的元素赋值为0

这里不用担心会将本来第一行或第一列的1改成了0,因为这些值最后注定要成为0的,比如matrix[i][j]==0,那么matrix[i][0]处在第i行,matrix[0][j]处于第j列,最后都要设置为0的。

3.根据第一行和第一列的信息,已经可以将剩下的矩阵元素赋值为结果所需的值了即,拿第一行为例,如果扫描到一个0,就将这一列都清0.

4.根据1中确定的状态,处理第一行和第一列。

如果最开始得到的第一行中有0的话,就整行清零。同理对列进行处理。

AC代码

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) { if (matrix.empty())
return; //求得所给矩阵的行数、列数
int m = matrix.size();
int n = matrix.front().size(); //初始化首行,首列标志位false,代表元素不为0
bool f_row = false, f_col = false; for (int j = 0; j < n; j++)
{
if (matrix[0][j] == 0)
{
f_row = true;
break;
}//if
}//for //记录首行、首列的状态
for (int i = 0; i < m; i++)
{
if (matrix[i][0] == 0)
{
f_col = true;
break;
}//if
}//for //下面用原矩阵的首行和首列作为本地存储空间,因为首行首列单独处理,所以下标均从1开始
for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{
//如果元素(i,j)为0,则将该元素对应的首行位置(0,j)以及首列位置(i,0)值赋为0
if (matrix[i][j] == 0)
{
matrix[0][j] = 0;
matrix[i][0] = 0;
}//if
}//for
}//for //下面根据首行,首列元素值,更新矩阵中的0
for (int j = 1; j < n; j++)
{
//找到元素为0的坐标,将矩阵中该列元素全部更改为0
if (matrix[0][j] == 0)
{
for (int i = 0; i < m; i++)
matrix[i][j] = 0;
}//if
} for (int i = 1; i < m; i++)
{
if (matrix[i][0] == 0)
{
for (int j = 0; j < n; j++)
matrix[i][j] = 0;
}//if
}//for //最后处理首行和首列
if (f_row)
{
for (int j = 0; j < n; j++)
matrix[0][j] = 0;
} if (f_col)
{
for (int i = 0; i < m; i++)
matrix[i][0] = 0;
} }
};

GitHub测试程序源码

LeetCode(73)Set Matrix Zeroes的更多相关文章

  1. LeetCode(73):矩阵置零

    Medium! 题目描述: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1], ...

  2. LeetCode(172)Factorial Trailing Zeroes

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

  3. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  4. LeetCode(54)Spiral Matrix

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

  5. Qt 学习之路 2(73):Qt 线程相关类

    Home / Qt 学习之路 2 / Qt 学习之路 2(73):Qt 线程相关类 Qt 学习之路 2(73):Qt 线程相关类  豆子  2013年11月26日  Qt 学习之路 2  7条评论 希 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

随机推荐

  1. hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

  2. size_t与size_type区别

    size()  标准库string里面有个函数size,用来返回字符串中的字符个数,具体用法如下: string st("The expense of spirit\n");cou ...

  3. c++ 常用的几种重载操作符

    运算符可以作为普通函数,朋友函数或成员函数来重载.下面的经验法则可以帮助您确定哪种形式最适合于给定的情况: 如果你重载了赋值(=),下标([]),函数调用(())或成员选择( - >),那么它就 ...

  4. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

    考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: #include<stdio.h> #inc ...

  5. 接口测试_RESTClient基本使用

    火狐浏览器插件RESTClient基本使用. 消息头: Content-Type : application/x-www-form-urlencoded

  6. LVS集群-DR模式

    同上个实验一样,还是准备三台机器 分发器(sishen_63):eth0 192.168.1.63 RealServer1sishen_64) RealServer2sishen_65) 首先配置网卡 ...

  7. C#基础学习4

    流程控制!

  8. python中的格式化字符

    python中的格式化字符在python中我们会遇到一个问题,问题是如何输出格式化的字符串.我们经常会输出类似'亲爱的xxx你好!你xx月的话费是xx,余额是xx'之类的字符串,而xxx的内容都是根据 ...

  9. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...

  10. Android模板制作

    本文详细介绍模板相关的知识和如何制作Android模版及使用,便于较少不必要的重复性工作.比如我在工作中如果要创建一个新的模块,就不要需要创建MVP相关的几个类:Model.View.Presente ...