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的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
随机推荐
- C#基础--属性
定义一个Book类: namespace ConsoleTest { public class Book { private string _bookIsbn; private string _boo ...
- 内存/硬盘/io关系
CPU:工人,干活的,判断以及逻辑处理 硬盘:仓库,原料,数据存储 内存:车间,工人干活的地方,车间中加工原料,当车间中没有原料了,在从仓库中取原料,对原料进行加工 内存本身有一定的存储空间,对内存 ...
- Ajax.BeginForm无法调用 ajaxOptions的js函数
使用ajax.beginForm无法调用ajaxOptions的js函数的原因,一般都是缺少以下2个JS文件: 1,Install-Package jQuery –version 1.10.22,In ...
- linux统计单词数
sort +awk+uniq 统计文件中出现次数最多的前10个单词 实例 cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sor ...
- Nodejs 学习
1,Node.js REPL交互式解释器:nodejs安装完毕后,打开终端,进入到nodejs的安装目录下,输入node,进入到新的页面,该页面称为Node.js REPL (交互式解释器):可以简单 ...
- wefwewewe
<a hred="https://www.baidu.com">dssf</a>
- centos7下用yum安装mysql5.7
1.安装mysql源 下载地址:http://dev.mysql.com/downloads/repo/yum/ 下载之后用yum安装:yum localinstall -y xx.noarch.rp ...
- 获取客户端IP地址经纬度所在城市
<?php $getIp=$_SERVER["REMOTE_ADDR"]; echo 'IP:',$getIp; echo '<br/>'; $content = ...
- C89, C99, C11: All the specifics that I know
before anything.. sizeof is an operand! sizeof is an operand! sizeof is an operand! 重要なことは三回にしませんね! ...
- poi做Excel数据驱动,支持.xls和.xlsx格式的excel文档,比起jxl强大不少
import java.io.FileInputStream;import java.io.InputStream;import java.util.Iterator;import java.util ...