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的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
随机推荐
- python3.5 修改 IIS WEB.CONFIG的相关方法
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- from xml.etree.ElementTree import ElementTree,Element ...
- html使用css让文字多行超出部分用省略号三个点显示的方法案例
text-overflow: -o-ellipsis-lastline;overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-w ...
- ORACLE AWR性能报告和ASH性能报告的解读
数据库的性能分析可分为会话级和系统级:如果确定某个会话存在性能问题,最常见的分析方式是对这个会话做一个SQL_TRACE或者10046事件,通过分析trace文件来定位问题所在.如果无法确定哪个会话性 ...
- 高效开发之SASS篇
作为通往前端大神之路的普通的一只学鸟,最近接触了一样稍微高逼格一点的神器,特与大家分享~ 他是谁? 作为前端开发人员,你肯定对css很熟悉,但是你知道css可以自定义吗?大家都知道,js中可以自定义变 ...
- PHP数组关于数字键名的问题
以下是对PHP数组数字键名的几点总结: 键名长度只能在 int 长度范围内,超过int 范围后将会出现覆盖等混乱情况 在键名长度为 int 范围内存取值时,PHP会强制将数字键名转换为 int 数值型 ...
- 2017中国数据库技术大会(DTCC)又要来啦!期待~~
2017第八届中国数据库技术大会(DTCC2017)将于2017年5月11-13日如约而至.2017中国数据库技术大会(DTCC)以"数据驱动•价值发现"为主题,汇集来自互联网.电 ...
- 花点时间搞清top、clientTop、scrollTop、offsetTop
[转贴]花点时间搞清top.clientTop.scrollTop.offsetTop scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前 ...
- 各种文件的ContentType
CONTENT_TYPE = { 16 '.load': 'text/html', 17 '.123': 'application/vnd.lotus-1-2-3', 18 '.3ds': 'imag ...
- 面试题-Java基础-垃圾回收
1.Java中垃圾回收有什么目的?什么时候进行垃圾回收? 垃圾回收的目的是识别并且丢弃应用不再使用的对象来释放和重用资源. 2.System.gc()和Runtime.gc()会做什么事情? 这两个方 ...
- 移动HTML 5前端性能优化指南(转载)
前端工程师的菜!最近移动Html 5越来越火,想有一个体验流畅的Html 5 应用,这篇优化指南就别放过咯.腾讯的同学将关键的注意点与优化方法都总结出来,全文高能干货,非常值得深度学习 >> ...