题目

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. 我的spring-boot开发环境

    我的spring-boot开发环境,目的方便我快速搭建开发环境,同时可以最佳实践.使用spring-boot 2.1.x. 代码地址:GitHub my-springboot-examples 目的是 ...

  2. redis启停

    1 查看服务器上启动的redis服务 开了两个redis单机服务,监听在两个端口. 开了一个集群,监听在6个端口. [root@localhost bin]# ps aux | grep redis ...

  3. redis过期策略+事务+分布式锁+单redis服务器锁

    过期策略 相关知识:redis 内存数据集大小上升到一定大小的时候,就会施行数据淘汰策略.redis 提供 6种数据淘汰策略: voltile-lru:从已设置过期时间的数据集(server.db[i ...

  4. Codeforces Round #542(Div. 2) D1.Toy Train

    链接:https://codeforces.com/contest/1130/problem/D1 题意: 给n个车站练成圈,给m个糖果,在车站上,要被运往某个位置,每到一个车站只能装一个糖果. 求从 ...

  5. New Land LightOJ - 1424

    New Land LightOJ - 1424 题意:找出01矩阵中最大的完全由0组成的矩阵. 方法: 重点在于转化. 先预处理(i,j)点向上最长能取到的连续的全0条的长度.然后枚举某一行作为矩阵的 ...

  6. Selenium中验证码处理

    验证码的主要实现方法 1.读取方式:在服务器目录下保存制作好的图片文件.然后在web页面上让用户识别,这种方式完全可以通过URL来破解图片的地址,通过图片地址可以间接的知道图片表示的验证码是什么所以这 ...

  7. 用IARIdePm新建STM8工程步骤

    IARdePm 如何新建工程及其调用库函数1.新建文件夹,例如,新建文件夹名字(不能为中文)为:Lib_test_GPIO_OUT2.新建工程,Create New Project...,选择Empt ...

  8. javaFx 学习笔记

    1.每个javaFx程序定义在一个继承自javafx.application.Application的类中 Button:用于设置一个按钮,Button btOK = new Button(" ...

  9. Linux在线安装pip和numpy

    最近写Python需要用到numpy包 运行pip install numpy就会自动安装 一.因此需要先安装pip 1.如果安装的是Python>=2.7.9或者Python>=3.4, ...

  10. P1179 数字统计

    题目描述 请统计某个给定范围[L, R]的所有整数中,数字 2 出现的次数. 比如给定范围[2, 22],数字 2 在数 2 中出现了 1 次,在数 12 中出现 1 次,在数 20 中出 现 1 次 ...