LeetCode 73. Set Matrix Zeros(矩阵赋零)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
click to show follow up.
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?
题目标签:Array
Java Solution:
Runtime beats 23.97%
完成日期:07/23/2017
关键词:Array
关键点:把原题给的十字改0的这一个过程分解成四个步骤来实现
public class Solution
{
public void setZeroes(int[][] matrix)
{
boolean firstRow = false;
boolean firstCol = false; // iterate the first row and column to see any zeros there
for(int y=0; y<matrix[0].length; y++)
{
if(matrix[0][y] == 0)
{
firstRow = true;
break;
}
} for(int x=0; x<matrix.length; x++)
{
if(matrix[x][0] == 0)
{
firstCol = true;
break;
}
} // iterate rest rows and columns,
//if see any zero, put zero in corresponding position in first row and first column
for(int x=1; x<matrix.length; x++) // rows
{
for(int y=1; y<matrix[0].length; y++) // columns
{
if(matrix[x][y] == 0)
{
matrix[x][0] = 0;
matrix[0][y] = 0;
}
}
} // iterate rest rows and columns again,
// for each position, if corresponding first row or first column has 0, change this to 0
for(int x=1; x<matrix.length; x++)
{
for(int y=1; y<matrix[0].length; y++)
{
if(matrix[x][0] == 0 || matrix[0][y] == 0)
matrix[x][y] = 0;
}
} // check two boolean firstRow and firstCol, and decide need to make first row and first column to 0
if(firstRow)
for(int y=0; y<matrix[0].length; y++)
matrix[0][y] = 0; if(firstCol)
for(int x=0; x<matrix.length; x++)
matrix[x][0] = 0;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/4341590.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 73. Set Matrix Zeros(矩阵赋零)的更多相关文章
- [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 ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- [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[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- Leetcode 54:Spiral Matrix 螺旋矩阵
54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...
- [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 ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] Reshape the Matrix 重塑矩阵
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 【LeetCode】Spiral Matrix(螺旋矩阵)
这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...
随机推荐
- shell脚本命令,一些你在书上找不到的命令。
1.!$<!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样: $mkdir mydir$mv mydir yourdir$cd yourdir 可以改成: $mkd ...
- oracle 建表空间->创建用户并把表空间分配给用户->给用户授权->导库
首先注意:我参考网上使用的sysdba模式(normal)登陆的,其他的模式建不了用户(个人没有进行其他模式的表空间尝试,如有人尝试欢迎补充,感激不尽) 表空间相当于表的容器(一下所有的操作都适用于o ...
- CentOS文件权限管理
目录 文件属性 chown更改所有者 chgrp更改所属组 文件权限rwx chmod修改权限 默认权限umask 权限判定的顺序 特殊权限SUID,SGID,sticky 隐藏权限chattr,ls ...
- POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)
Problem Description In this problem, you have to analyze a particular sorting algorithm. The algorit ...
- oracle 删除外键约束 禁用约束 启用约束
oracle 删除外键约束 禁用约束 启用约束 执行以下sql生成的语句即可 删除所有外键约束 Sql代码 select 'alter table '||table_name||' drop con ...
- mybatis-主配置文件介绍
mybatis下载地址:http://code.google.com/p/mybatis/ 学习手册地址:http://mybatis.github.io/mybatis-3/zh/index.htm ...
- [UIKit学习]03.关于UILable
代码创建UILabel UILabel *label = [[UILabel alloc] init]; label.text = @"单肩包"; label.frame = CG ...
- 【设计模式】module(模块)模式
写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...
- 当你的SSM项目中的springmvc.xml发生第一行错误解决方案
当你新建了一个SSM项目,你复制网上的xml文件来配置或者你下载了一个SSM项目打开发现xml文件错误,打开是第一行报错的时候你是不是很懵逼 或者是这样 总之就是xml文件中<?xml vers ...
- IDEA——IDEA使用Tomcat服务器出现乱码问题
最近刚使用IDEA,在开发一个功能的时候,开始使用Jetty作为容器进行web项目开发,测试通过.然后想了一下线上服务器使用的容器是Tomcat,还是用Tomcat跑一下项目在测试一下,本地和服务器使 ...