题目:

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. 5) mvn archetype:generate

    获取帮助 mvn -h 命令格式 usage: mvn [options] [<goal(s)>] [<phase(s)>] -D,--define <arg> D ...

  2. jaxws.xsd

    示例: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://ww ...

  3. C++ 中的运算符的优先级

    C++ 中的运算符的优先级 直接看图

  4. delphi TStringList 用法详解

    转自: http://blog.163.com/you888@188/blog/static/67239619201472365642633/ delphi TStringList 用法详解 2014 ...

  5. Python学习-17.Python中的错误处理(二)

    错误是多种多样的,在 except 语句中,可以捕获指定的异常 修改代码如下: import io path = r'' mode = 'w' try: file = open(path,mode) ...

  6. html5 app开发

    如今html5技术越来越成熟,很多iPhone 及Android 上的移动APP都能用html5来开发完成.让我们一起来了解一下html5开发app. 一.HTML5框架开发的移动APP 编写开发游戏 ...

  7. ionic xcode8 App上传应用详细流程

    第一步: 进入开发者官网 https://developer.apple.com   2.证书 序号1:开发者证书,用于真机调试   序号2:上传证书,用于发布最终版 3.证书申请 由于我现在是要发布 ...

  8. RabbitMQ基础入门篇

    下载安装 Erlang RabbitMQ 启动RabbitMQ管理平台插件 DOS下进入到安装目录\sbin,执行以下命令 rabbitmq-plugins enable rabbitmq_manag ...

  9. wpf datagrid 隔行变色

    <DataGrid AlternationCount="2"> <DataGrid.RowStyle> <Style TargetType=" ...

  10. Mutual Training for Wannafly Union #6 E - Summer Trip(并查集)

    题目链接:http://www.spoj.com/problems/IAPCR2F/en/ 题目大意: 给m个数字代表的大小,之后n组数据,两两关联,关联后的所有数字为一组,从小到大输出组数以及对应的 ...