给定一个矩阵,把零值所在的行和列都置为零。例如:

1 2 3

1 3

1 1 1

操作之后变为

1 3

0 0 0

1 1

方法1:

赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零。额外空间为O(m*n).

方法2:

两个数组,bool[m] 和 bool[n] 分别存某行有零,后者某列有零。之后根据数组值将原矩阵相应位置置零。额外空间O(m + n)。

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
int len1 = matrix.size();
if (len1 == ) return ;
int len2 = matrix[].size();
if (len2 == ) return ;
vector<bool > row(len1), col(len2);
for (int i = ; i < len1; i++)
for (int j = ; j < len2; j++)
{
if (matrix[i][j] == )
{
row[i] = true; col[j] = true;
}
}
for (int i = ; i < len1; i++)
for (int j = ; j < len2; j++)
{
if (row[i] == true)
matrix[i][j] = ;
else if (col[j] == true)
matrix[i][j] = ;
}
return ;
}
};

方法3:(常数额外空间)

1. 找到一个零的位置,把这行这列当做方法2中的两个数组存值。

2. 根据1的位置的所在行和列的值是否有零将矩阵相应位置置零。

3. 再把1中零所在位置的行和列置零。

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
int len1 = matrix.size();
if (len1 == ) return ;
int len2 = matrix[].size();
if (len2 == ) return ;
int row = -, col = -;
for (int i = ; i < len1; i++)
for (int j = ; j < len2; j++)
{
if (matrix[i][j] == )
{
row = i; col = j;
}
}
if (row == -) return;
for (int i = ; i < len1; i++)
for (int j = ; j < len2; j++)
{
if (matrix[i][j] == && i != row && j != col)
{
matrix[i][col] = ;
matrix[row][j] = ;
}
}
for (int i = ; i < len1; i++)
for (int j = ; j < len2; j++)
{
if (i != row && j != col)
{
if (matrix[i][col] == )
matrix[i][j] = ;
else if (matrix[row][j] == )
matrix[i][j] = ;
}
}
int index = -;
while(++index < len1) matrix[index][col] = ;
index = -;
while(++index < len2) matrix[row][index] = ;
return ;
}
};

2015/03/25:

Python:

用第一行和第一列记录这一行和这一列中是否有零,当然,一开始要先用row和col记录第一行和第一列是否有零,最后再根据这个判断是否将第一行第一列置零

class Solution:
# @param matrix, a list of lists of integers
# @return nothing (void), do not return anything, MODIFY matrix IN PLACE.
def setZeroes(self, matrix):
if len(matrix) == 0:
return ;
row, col = 1, 1
for i in range(0, len(matrix)):
if matrix[i][0] == 0:
col = 0
for j in range(0, len(matrix[0])):
if matrix[0][j] == 0:
row = 0 for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0 if col == 0:
for i in range(0, len(matrix)):
matrix[i][0] = 0
if row == 0:
for j in range(0, len(matrix[0])):
matrix[0][j] = 0

leetcode[73] Set Matrix Zeroes 将矩阵置零的更多相关文章

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

  2. 力扣—set matrix zeroes (矩阵置零) python实现

    题目描述: 中文: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 英文: Given a m x n matrix, if an eleme ...

  3. LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)

    题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. ...

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

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

  6. Leetcode#73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  7. 73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0

    [抄题]: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. ...

  8. LeetCode:矩阵置零【73】

    LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   ...

  9. Java实现 LeetCode 73 矩阵置零

    73. 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ...

随机推荐

  1. 《Programming Hive》读书笔记(一)Hadoop和hive环境搭建

    <Programming Hive>读书笔记(一)Hadoop和Hive环境搭建             先把主要的技术和工具学好,才干更高效地思考和工作.   Chapter 1.Int ...

  2. jQuery、Ajax分页

    1.效果预览 2.HTML代码 <div class="row"> <div class="col-lg-12 col-sm-12 col-xs-12 ...

  3. 创意HTML5文字特效 类似翻页的效果

    原文:创意HTML5文字特效 类似翻页的效果 之前在网上看到一款比较有新意的HTML5文字特效,文字效果是当鼠标滑过是出现翻开折叠的效果,类似书本翻页.于是我兴致勃勃的点开源码看了一下,发现其实实现也 ...

  4. The Toast in android

    Toast can show the help/prompts to user. There have five effect of toast as bellow: 1.default effect ...

  5. Zend_Db_Table::getDefaultAdapter is not working

    在Bootstrap中使用 $url = constant ( "APPLICATION_PATH" ) . DIRECTORY_SEPARATOR . 'configs' . D ...

  6. Linux(CentOS)系统下安装好apache(httpd)服务后,其他电脑无法访问的原因

    原文:Linux(CentOS)系统下安装好apache(httpd)服务后,其他电脑无法访问的原因 今天试了下在虚拟机上利用CentOS系统的yum命令安装好了httpd(apache2.4.6), ...

  7. 二叉搜索树(Binary Search Tree)--C语言描述(转)

    图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...

  8. ABP领域层——领域事件(Domain events)

    ABP领域层——领域事件(Domain events) 基于DDD的现代ASP.NET开发框架--ABP系列之14.ABP领域层——领域事件(Domain events) ABP是“ASP.NET B ...

  9. SharePoint 如何使自己的网页自动跳转

    SharePoint 如何使自己的网页自动跳转         SharePoint自动制作自己的网页跳的很easy,只有在页面上要添加一个Web部分--内容编辑器,对应的js代码就可以.       ...

  10. 朴素贝叶斯算法(Naive Bayes)

    朴素贝叶斯算法(Naive Bayes) 阅读目录 一.病人分类的例子 二.朴素贝叶斯分类器的公式 三.账号分类的例子 四.性别分类的例子 生活中很多场合需要用到分类,比如新闻分类.病人分类等等. 本 ...