题目:

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.

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?

思路:

O(mn)解法:用m*n的空间记录每个某个位置是否要设置成0。

O(m+n)解法:用一个数组记录某一列是否需要设置成0,用一个数组记录某一行是否需要设置成0。

常数空间解法:如果我们可以用matrix本身的空间来记录,就不需要额外的空间了。用matrix第一行记录相应的列是都需要置0,第一列来记录相应的行是否需要置0。用来记录之前需要提前判断第一行和第一列是否需要置0。然后遍历matrix(出了第一行和第一列)如果matrix[i][j]等于0,那么matrixp[i][0] = 0 (行标志),matrix[0][j] = 0(列标志)。再次遍历matrix只要对应位置的行标志或者列标志等于0,相应的位置就置0,。最后还要看第一行和第一列是否需要置0。

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
var m=matrix.length,n=matrix[0].length;
var firstC=1,firstR=1;
for(var i=0;i<m;i++){
if(matrix[i][0]==0){
firstC=0;
}
}
for(var i=0;i<n;i++){
if(matrix[0][i]==0){
firstR=0;
}
} for(var i=1;i<m;i++){
for(j=1;j<n;j++){
if(matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(var i=1;i<m;i++){
for(j=1;j<n;j++){
if(matrix[i][0]==0||matrix[0][j]==0){
matrix[i][j]=0;
}
}
} if(firstC==0){
for(var i=0;i<m;i++){
matrix[i][0]=0;
}
}
if(firstR==0){
for(var i=0;i<n;i++){
matrix[0][i]=0;
}
} };

【数组】Set Matrix Zeroes的更多相关文章

  1. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  2. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

  3. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  4. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

  5. [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 ...

  6. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

  7. [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 ...

  8. 【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. 思路:不能用 ...

  9. 【Leetcode】【Medium】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. 解题思路: ...

随机推荐

  1. 201521123014《Java程序设计》第1周学习总结

    201521123014 java第一周总结 1.本周学习总结 刚认识这一门新语言,我就充满了好奇心,想看看Java和学过C语言,C++有什么区别.在这一周的学习中,我认识到,对于初学者而言,Java ...

  2. Codeforces777B Game of Credit Cards 2017-05-04 17:19 29人阅读 评论(0) 收藏

    B. Game of Credit Cards time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. HDU1078 FatMouse and Cheese(DFS+DP) 2016-07-24 14:05 70人阅读 评论(0) 收藏

    FatMouse and Cheese Problem Description FatMouse has stored some cheese in a city. The city can be c ...

  4. 使用sos查看.NET对象内存布局

    前面我们图解了.NET里各种对象的内存布局,我们再来从调试器和clr源码的角度来看一下对象的内存布局.我写了一个测试程序来加深对.net对象内存布局的了解: using System; using S ...

  5. SQL Server 2016最值得关注的10大新特性

    全程加密技术(Always Encrypted) 全程加密技术(Always Encrypted)支持在SQL Server中保持数据加密,只有调用SQL Server的应用才能访问加密数据.该功能支 ...

  6. 使用ABP框架踩过的坑系列4

    数据库连接和事务管理,是数据库应用中的最重要概念之一.做过的人,都会头疼:何时Open一个连接?何时Start一个事务?何时Dispose这个连接?... ABP框架试图用一个叫做UnitOfWork ...

  7. C#时间加减

    DateTime dt =......//减数DateTime dt_n = DateTime.Now;//被减数 long x = dt .ToFileTime();//表示自协调世界时 (UTC) ...

  8. (二)Mahapps标题栏

    一.MetroWindow 是什么? 1.默认的MetroWindow由以下几部分组成: (1)标题栏的显示/不显示: ShowTitleBar="False" (2)调节柄并不是 ...

  9. CentOS 7 - 安装Windows字体!

    1,安装cabextract: 下载地址:http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/cabe ...

  10. leetcode 91. 解码方法 JAVA

    题目: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. ...