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 ...
随机推荐
- jQuery学习小结1-CSS操作+事件
一.DOM对象和jQuery 对象互换 1.jQuery对象 就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的,其可以使用jQuery里的方法.比如: $(&quo ...
- 转载python2进制打包相关
Python模块——struct(字节流,组包拆包实现) http://www.linuxidc.com/Linux/2014-02/97158.htm [日期:2014-02-24] 来源:Linu ...
- FileUpload上传与下载
后台代码: public string connstr = "server=128.1.3.113;database=test;uid=sa;pwd=pass"; protecte ...
- Linux下配置用msmtp和mutt发邮件
Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...
- 使用ContentProvider管理多媒体-----查看多媒体数据中的所有图片
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; impo ...
- String类的写时拷贝
#include<iostream>using namespace std; class String;ostream& operator<<(ostream & ...
- Java 类的一般特征
1. 创建类的对象时的内存结构 用图来解释: 使用new 创建 a1 时,成员变量的值都是初始默认值. 然后显式的改变其属性的值. 创建a3 时,a3 是直接指向 a1, 即a3 = a1, 两个对象 ...
- AS3事件流机制
事件流: 显示对象,深度 MouseEnabled,MouseChildren:显示对象,同层次(父容器为同一对象)遮挡问题
- MATLAB图像处理函数汇总(二)
60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...
- windows操作系统日常使用
快捷键使用: 看实例,学经验,我看行. 1.人体学输入设备被禁用怎么办(鼠标被禁用.键盘被禁用) 有一天脑子抽风,把鼠标给禁用了.以前不常用快捷键,这下必须学学怎么使用快捷键了,现在记下来,防止以后脑 ...