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 ...
随机推荐
- MySQL的JOIN(五):JOIN优化实践之排序
这篇博文讲述如何优化JOIN查询带有排序的情况.大致分为对连接属性排序和对非连接属性排序两种情况.插入测试数据. CREATE TABLE t1 ( id INT PRIMARY KEY AUTO_I ...
- python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)
---恢复内容开始--- 一.python单元测试实例介绍 unittest框架又叫PyUnit框架,是python的单元测试框架. 先介绍一个普通的单元测试(不用unittest框架)的实例: 首先 ...
- pig报错
pig failed to read data from....... 错误可能1:load data的目录不在,或者引用出错,load data '/in/train'这里的红色/应该去掉,因为默认 ...
- Android 8.0 功能和 API
Android 8.0 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 用户体验 通知 在 Android 8.0 中,我们已重新设计通知,以便为管理通知行为和设置提供更轻松和更统一的 ...
- Docker入门之三容器
上一篇博客学习了下镜像,今天来学习容器.容器类似一个手机中的沙盒环境,用来运行app实例.和镜像一样也是对容器的创建.删除.导出等. 由于我买的参考书中的例子好多都是基于linux的,所以我将dock ...
- bzoj1036 [ZJOI2008]树的统计
一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从 ...
- Nunit测试工具使用实例
前言: 本文主要是介绍了Nunit的基本使用,其中参详了很多已有的文章,由于最近要使用其进行测试,所以对网上的文章做了下整理,同时加入了一些自己的实践. NUnit的属性 TestFixture 它标 ...
- python之HTMLParser解析HTML文档
HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...
- 动态IP解析
本文介绍两种方便获取主机动态IP的方式(DDNS,IP报告网页),并给出相应的代码实现. shell脚本获取本机IP,执行上传操作和更新DNS操作.定期执行通过crontab或者systemd等服务. ...
- 新版MySql 5.6.20,安装后无法登陆的解决办法
1.按照提示安装好mysql 2.运行cmd 进入mysql的安装目录,我的安装目录C:\Program Files\MySQL\MySQL Server 5.6\bin 输入 cd C:\Progr ...