Leetcode 073 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.
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?
1.O(m+n)space,两个数组m和n分别存储第i行和第j列有没有零,比较慢
2.constant space ,利用矩阵的第零行和第零列分别存储第i行和第j列有没有零,需要注意的是再用两个变量row和column来表示第零行和第零列是否有零存在,因为这是边界情况
public class S073 {
public void setZeroes(int[][] matrix) {
//O(m+n) space slow
/* int [] m = new int[matrix.length];
int [] n = new int[matrix[0].length];
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
m[i] = -1;
n[j] = -1;
}
}
}
for (int i = 0;i<matrix.length;i++) {
if (m[i] == -1) {
for (int j = 0;j<matrix[0].length;j++) {
matrix[i][j] = 0;
if (n[j] == -1) {
for (int k = 0;k<matrix.length;k++) {
matrix[k][j] = 0;
}
}
}
}
} */
//利用矩阵的第零行和第零列分别来存每一列和每一行是否有零存在
int row = -1;
int column = -1;
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
if (i == 0 ) {
row = 0;
}
if (j == 0) {
column = 0;
}
matrix[0][j] = 0; //第零行
matrix[i][0] = 0; //第零列
}
}
}
for (int i = 1;i<matrix.length;i++) {
if (matrix[i][0] == 0) {
for (int j = 1;j<matrix[0].length;j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1;j<matrix[0].length;j++) {
if (matrix[0][j] == 0) {
for (int i = 1;i<matrix.length;i++) {
matrix[i][j] = 0;
}
}
}
if (row == 0) {
for (int i = 0;i<matrix[0].length;i++) {
matrix[0][i] = 0;
}
}
if (column == 0) {
for (int i = 0;i<matrix.length;i++) {
matrix[i][0] = 0;
}
}
}
}
Leetcode 073 Set Matrix Zeroes的更多相关文章
- Java for LeetCode 073 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. 解题思路: ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] 73. 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. Exampl ...
- Leetcode#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
- [LeetCode] 73. 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. Follow ...
- 【Leetcode】Set Matrix Zeroes
给定一个m x n的矩阵,如果某个元素为0,则把该元素所在行和列全部置0. Given a m x n matrix, if an element is 0, set its entire row a ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
随机推荐
- 浅谈javascript中stopImmediatePropagation函数和stopPropagation函数的区别
在事件处理程序中,每个事件处理程序中间都会有一个event对象,而这个event对象有两个方法,一个是stopPropagation方法,一个是stopImmediatePropagation方法,两 ...
- 闹心的python编码
说起编码,真是十分忧伤.每次听课都是绕了半天把自己搞糊涂.今天特意来整理一下思路. What 编码!? 基本概念很简单.首先,我们从一段信息即消息说起,消息以人类可以理解.易懂的表示存在.我打算将这种 ...
- 安卓平台多个视频叠加演示demo说明
多个音视频编辑演示说明: 第一个-----字幕和视频的叠加: 说明: 把字幕文件中的文字,按照时间叠加到视频上去,形成新的视频. 类似我们看电影时的字幕. 下载地址:http://www.cnblog ...
- JavaScript进阶(一)
OK接下来,我们再次梳理一遍js并且提高一个等级. 众所周知,web前端开发者需要了解html和css,会只用html和css创建一个漂亮的页 面,但是这肯定是不够的,因为它只是一个静态的页面,我们 ...
- 内存/硬盘/io关系
CPU:工人,干活的,判断以及逻辑处理 硬盘:仓库,原料,数据存储 内存:车间,工人干活的地方,车间中加工原料,当车间中没有原料了,在从仓库中取原料,对原料进行加工 内存本身有一定的存储空间,对内存 ...
- lda 主题模型--TOPIC MODEL--Gibbslda++结果分析
在之前的博客中已经详细介绍了如何用Gibbs做LDA抽样.(http://www.cnblogs.com/nlp-yekai/p/3711384.html) 这里,我们讨论一下实验结果: 结果文件包括 ...
- sql注入示例
实验指导说明 实验环境 • 实验环境 o 操作机:Windows XP o 目标机:Windows 2003 o 目标网址:www.test.ichunqiu • 实验工具: Tools Path S ...
- ubuntu 开机进入不了图形界面
在开机的时候有注意到空间不足.第二天重启的时候进入不了系统. 但是 ctrl + alt _F6 可以进入shell . 于是估计是空间不足导致进入不了系统. 找到哪里文件夹空间异常的大就可以解决问 ...
- HTML canvas图像裁剪
canvas drawImage方法的图像裁剪理解可能会比较耗时,记录一下,以便供人翻阅! context.drawImage(img,sx,sy,swidth,sheight,x,y,width,h ...
- Scrapy框架使用—quotesbot 项目(学习记录一)
一.Scrapy框架的安装及相关理论知识的学习可以参考:http://www.yiibai.com/scrapy/scrapy_environment.html 二.重点记录我学习使用scrapy框架 ...