【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. 题解:因为题 ...
随机推荐
- 第五百八十天 how can I 坚持
一定要稳住啊,怎么感觉心神不宁呢.哎.越是这种情况越能考验一个人吧. 说都会说,做起来真的好难啊. 今天上班一天都感觉心神不宁的.到底是哪出了问题,事情太多了.好吧,是挺多的,考研.上班,还得考虑结婚 ...
- C# 读取Excel内容
一.方法 1.OleD方法实现该功能. 2.本次随笔内容只包含读取Excel内容,并另存为. 二.代码 (1)找到文档代码 OpenFileDialog openFile = new OpenFile ...
- RSA加密前端JS加密,后端asp.net解密,报异常
RSA加密前端JS加密,后端asp.net解密,报异常 参考引用:http://www.ohdave.com/rsa/的JS加密库 前端JS加密代码: function GetChangeStr() ...
- Repeater控件 ---表格展示数据
简介: Repeater控件是Web 服务器控件中的一个容器控件,它使您可以从页的任何可用数据中创建出自定义列表. Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repea ...
- C/C++ 活动预处理器
错误 1 fatal error C1083: 无法打开包括文件:“iec/i.h”: No such file or directory #ifdef SUPPROT_IEC61850 #inclu ...
- Unix网络编程 -- ubuntu下搭建编译环境( 解决unp.h 编译等问题)
1.安装编译器,安装build-essential sudo apt-get install build-essential 2.下载本书的头文件 下载unpv13e http://ishare.i ...
- linux关闭防火墙
查看防火墙状态: sudo service iptables status linux关闭防火墙命令: sudo service iptables stop linux启动防火墙命令: sudo se ...
- 如何在MAC上使用SVN,简单几行命令搞定
如果你要使用SVN管理代码,如果不是迫不得已,还是看看git吧,但是SVN也要知道怎么用,原理是相似的. 首先,要准备的东西:svnX软件 https://pan.baidu.com/s/1mhEay ...
- 屏蔽input导致的回车提交事件
onkeypress="if(event.keyCode == 13) return false;"
- VS经常报错的link error 2019
VS经常报错的link error 2019 原因如下: 可能是找得到头文件,但是相关的dll或者lib找不到,需要在配置里面添加相应的库文件. project=>configuration.. ...