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 ...
随机推荐
- 多线程:head first Thread.join()
不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...
- hadoop运行wordcount实例,hdfs简单操作
1.查看hadoop版本 [hadoop@ltt1 sbin]$ hadoop version Hadoop -cdh5.12.0 Subversion http://github.com/cloud ...
- kvm 虚拟化的使用
kvm原理:基于内核空间虚拟化,加载内核模块,来做到虚拟化(简称内核空间).基于qemu连接内核,driver驱动连接kvm的API接口(简称用户空间): hypervisor 管理硬件设备,传统的虚 ...
- Thinkphp3.2版本使用163邮箱发(验证码)邮件
今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文. ------------------------------------------- ...
- XtraGrid滚轮翻页
滚轮翻页与传动的翻页更为方便,经过本人一番探讨与琢磨终于在XtraGrid的GridView中实现了鼠标滚轮翻页. 我新建了一个组件继承原本的GridControl,在组件中添加了一个ImageLis ...
- js 递归函数的使用及常用函数
1.递归函数的使用: 公园里有一堆桃子,猴子每天吃掉一半,挑出一个坏的扔掉,第6天的时候发现还剩1个桃子,问原来有多少个桃子 var peache;function peaches(n) { if ( ...
- 再起航,我的学习笔记之JavaScript设计模式28(委托模式)
## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...
- 【NOIP2016提高组day2】蚯蚓
那么我们开三个不上升队列, 第一个记录原来的蚯蚓, 第二个记录乘以p的蚯蚓 第三个记录乘以(1-p)的蚯蚓, 在记录每条就要入队列的时间,就可以求出增加的长度 每次比较三个队列的队首,取最大的值x的切 ...
- js自执行函数写法
(1)写法1 (function(){ //函数内容 })() (2)写法2 (function(){ //函数内容 }())
- Java中的类型擦除与桥方法
类型擦除 Java在语法中虽然存在泛型的概念,但是在虚拟机中却没有泛型的概念,虚拟机中所有的类型都是普通类.无论何时定义一个泛型类型,编译后类型会被都被自动转换成一个相应的原始类型. 比如这个类 pu ...