LeetCode 第 73 题 (Set Matrix Zeroes)

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 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?

既然不让占用额外的空间,那我们就做的极致一点,整个代码中只建立了六个局部变量。除此之外不再使用任何存储空间。

代码的思路很简单,找到矩阵的第一个零元素。那么这个零元素所在的行和列都是要被清零的。所以我们可以用这个 0 元素所在的行和列来存储其他的 0 元素的位置。记录完所有的零元素的位置后,就可以清零工作了。唯一需要注意的是这一特殊的行和列要最后处理,因为提早把它清零了我们就失去其他零元素的位置信息了。

下面是代码:

void getFirstZerosPos(const vector<vector<int>>& matrix, int &row_, int &col_)
{
    int rows = matrix.size();
    int cols = matrix[0].size();
    for( int row = 0; row < rows; row++ )
    {
        for(int col = 0; col < cols; col ++ )
        {
            if(matrix[row][col] == 0)
            {
                col_ = col;
                row_ = row;
                return;
            }
        }
    }
    col_ = -1;
    row_ = -1;
    return;
}

void setZeroes(vector<vector<int>>& matrix)
{
    int ROWS = matrix.size();
    if(ROWS == 0) return;
    int COLS = matrix[0].size();
    int col0, row0;
    getFirstZerosPos(matrix, row0, col0);
    if(row0 == -1) return;
    for( int row = row0; row < ROWS; row++ )
    {
        for(int col = 0; col < COLS; col ++ )
        {
            if(matrix[row][col] == 0)
            {
                matrix[row][col0] = 0;
                matrix[row0][col] = 0;
            }
        }
    }
    for(int col = 0; col < COLS; col ++ )
    {
        if(col == col0) continue;
        if(matrix[row0][col] == 0)
        {
            for(int row = 0; row < ROWS; row++)
            {
                matrix[row][col] = 0;
            }
        }
    }
    for(int row = 0; row < ROWS; row++) //遍历这一列的各行
    {
        if(row == row0) continue;
        if(matrix[row][col0] == 0) //如果这行对应的元素为 0
        {
            for(int col = 0; col < COLS; col ++ ) //就将这一行全写为 0
            {
                matrix[row][col] = 0;
            }
        }
    }
    for(int row = 0; row < ROWS; row++)
    {
        matrix[row][col0] = 0;
    }
    for(int col = 0; col < COLS; col++)
    {
        matrix[row0][col] = 0;
    }
}

时间: 2024-10-14 18:28:38

LeetCode 第 73 题 (Set Matrix Zeroes)的相关文章

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. Do it in-place. 翻译: 给定一个m x n矩阵,如果一个元素是0,就把它的整行和列设为0. 要求:就地置0. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],  

leetcode || 73、Set Matrix Zeroes

problem: 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 improve

Leetcode 细节实现题 Spiral Matrix

Spiral Matrix Total Accepted: 12721 Total Submissions: 62094My Submissions Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [

Leetcode 细节实现题 Spiral Matrix II

Spiral Matrix II Total Accepted: 12773 Total Submissions: 41526My Submissions Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2,

【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. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

Set Matrix Zeroes leetcode java

题目: 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

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

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. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

leetcode第一刷_Set Matrix Zeroes

这个题乍一看很简单,实际上还挺有技巧的.我最开始的想法是找一个特殊值标记,遇到一个0,把他所对应的行列中非零的元素标记成这个特殊值,0值保持不变,然后再从头遍历一次,碰到特殊值就转化成0. 问题是这个特殊值怎么确定,题目中没有把取值范围给出,我怀着侥幸的心理用了最大和最小的int,都被揪了出来..如果找一个不存在于数组中的值,这个复杂度太高了. 有没有其他更好的方法呢?当然有.这个思想很巧妙,最后的结果是把所有0所在的行列都化成0,换句话说,化成0这个事情只要标记出是哪一行以及哪一列就可以了,能