【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.
解题思路:
题目乍一看很简单,将矩阵当前为0的位的行列都置为0;
问题在于:当遇到一个已知0时,不能立刻将其行列置0,因为这样会把原本不是0的位更改,导致继续遍历的时候无法区分哪些是初始0,哪些是后改0;
有一个解决方法,就是先遍历所有位,记录所有初始为0的行列坐标,遍历一遍之后,再统一做更改;
但是这个解决方法会占用额外的空间,如何使用o(1)空间完成置0的任务?
解决方法:
1、先找到一个初始0位置,并记录它的行oi和列oj;
2、oi行和oj列最终需要置零,索性将oi这一行和oj这一列当做记录数组;
3、遍历所有位置,如果遇到0,则将其i和j对应的(oi, j)和(i, oj)置0,这样不会多置0,也做到了标记的作用;
4、最终遍历oi这一行,将值为0的位置所在列置0;同理遍历oj这一列的元素,将其值为0的位置所在行置0;
代码:
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
if (m == )
return;
int n = matrix[].size();
int oi = -, oj = -;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (matrix[i][j] == ) {
oi = i;
oj = j;
break;
}
}
if (oi != -) break;
}
if (oi == -) return;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (matrix[i][j] == ) {
matrix[oi][j] = ;
matrix[i][oj] = ;
}
}
}
for (int i = ; i < m; ++i) {
if (i != oi && matrix[i][oj] == ) { // notice
for (int j = ; j < n; ++j)
matrix[i][j] = ;
}
}
for (int j = ; j < n; ++j) {
if (matrix[oi][j] == ) {
for (int i = ; i < m; ++i)
matrix[i][j] = ;
}
}
for (int j = ; j < n; ++j)
matrix[oi][j] = ;
return;
}
};
【Leetcode】【Medium】Set Matrix Zeroes的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- 【LeetCode每天一题】Spiral Matrix(螺旋打印数组)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【leetcode刷题笔记】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. 题解:因为题 ...
随机推荐
- TJI读书笔记12-接口
TJI读书笔记12-接口 抽象类和抽象方法 接口 完全解耦和策略模式 接口间的继承关系 工厂模式 乱七八糟不知道怎么归类的知识点 接口和抽象类为我们提供了更强又有力的接口和实现分离的方法. 抽象类和抽 ...
- python异常处理、反射、socket
一.isinstance 判断对象是否为类的实例 n1 = print isinstance(n1,int) class A: pass class B(A): pass b= B() print i ...
- c# & Fizzler to crawl web page in a certain website domain
使用fizzler [HtmlAgilityPackExtension]和c#进行网页数据提取:fizzler是HtmlAgilityPack的一个扩展,支持jQuery Selector: 提取数据 ...
- git服务器的搭建
http://blog.jobbole.com/25944/ 1,概念 git服务器:就是一个仓储,一个大家都可以访问的公共仓储,大家可以从这个仓储中拉取和推送数据. 协议: 与gist服务通讯的仓储 ...
- laravel 数据库迁移
问题:之前有创建迁移文件 并且执行过 如果删除迁移文件 再重新创建迁移文件时就有问题 提示找不到之前的迁移文件 /** 一开始执行的命令 php artisan make:migration crea ...
- tomcat各种问题汇总
1. 让Tomcat支持中文路径名和中文文件名 因为内置get协议中的URL编码都是ISO-8859-1,所以需要我们强制编码,在tomcat/conf/Server.xml中添加URIEncodin ...
- docker 安装
Docker使用了一种叫AUFS的文件系统,这种文件系统可以让你一层一层地叠加修改你的文件,最底下的文件系统是只读的,如果需要修改文件,AUFS会增加一个可写的层(Layer),这样有很多好处,例如不 ...
- Ubuntu 14.04下搭建Python3.4 + PyQt5.3.2 + Eric6.0开发平台
引言 找了很多Python GUI工具集,还是觉得PyQt比较理想,功能强大跨平台,还支持界面设计器.花一天时间折腾了Ubuntu14.04(32位)+ Python3.4 + Qt5.3.2 + P ...
- Oracle Database常用补丁集Patch号及各版本PSU
Oracle Database常用补丁集Patch号及各版本PSU------------------------------------------------------------------- ...
- NVelocity 表格行奇偶样式变换
#foreach($test in $tests) #even <tr class="Test1"> #odd <tr class="Test2&quo ...