LeetCode Set Matrix Zeroes(技巧+逻辑)
题意:
给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0。(注:新置的0不必操作)
思路:
主要的问题是怎样区分哪些是新来的0?
方法(1):将矩阵复制多一个,根据副本来操作原矩阵。
方法(2):发现空间还可以用O(n)来解决。
方法(3):若m[i][j]=0,则将m[i][0]和m[0][j]标记为0,表示i行和j列都为0,但是这样的问题是,首行和首列会冲突?那就将首列先预处理一下,用另外的常量标记就行了。时间复杂度O(n*m),空间O(1)。
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int col0=, n=matrix.size(), m=matrix[].size();
for(int i=; i<n; i++)
{
if(!matrix[i][]) col0=;
for(int j=; j<m; j++)
{
if(!matrix[i][j])
matrix[i][]=matrix[][j]=;
}
}
for(int i=n-; i>=; i--)
{
for(int j=m-; j>; j--)
{
if(!matrix[][j]||!matrix[i][])
matrix[i][j]=;
}
if(col0==) matrix[i][]=;
}
}
};
AC代码
LeetCode Set Matrix Zeroes(技巧+逻辑)的更多相关文章
- LeetCode: Set Matrix Zeroes 解题报告
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...
- [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. click ...
- [leetcode]Set Matrix Zeroes @ Python
原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...
- LeetCode OJ--Set Matrix Zeroes **
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...
- 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. 原题链接:h ...
- [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. click ...
- 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
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
随机推荐
- Remote Debugging Chrome 结合Genymotion模拟器的移动端web真机调试(转)
尝试了好多方法,刚开始想用bluestacks调试手机页面,不过在打开chrome的时候,会黑屏,什么也看不了.最后又是更新android系统,又是卸掉bluestacks重新安装,怎么都不行.最后没 ...
- thinkphp 调用系统的方法
在需要调用的脚本 加载 load('filename');//filename为文件名
- ACTIVITI 表结构数据分析
ACTIVITI ACT_RU_EXECUTION 表 这个表是工作流程的核心表,流程的驱动都和合格表有密切的关系. 一般来讲一个流程实例都有一条主线.如果流程为直线流程,那么流程实例在这个表 ...
- The C10K problem
原文链接:http://www.kegel.com/c10k.html It's time for web servers to handle ten thousand clients simulta ...
- FloatingActionButton增强版,一个按钮跳出多个按钮--第三方开源--FloatingActionButton
FloatingActionButton项目在github上的主页:https://github.com/futuresimple/android-floating-action-button F ...
- 【转】HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑)
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- JAVA与指针
首先,提个问题:JAVA中没有指针,JAVA中有指针,哪个一个对呢? 答:都对,JAVA中没有指针,因为我们不能对指针直接操作,像C++那样用->来访问变量. JAVA有指针,因为JDK中封装了 ...
- bzoj 2037: [Sdoi2008]Sue的小球
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struc ...
- bzoj 1911: [Apio2010]特别行动队
#include<cstdio> #include<iostream> #define M 1000009 #define ll long long using namespa ...
- hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...