073 Set Matrix Zeroes

073 Set Matrix Zeroes

这道题唯一有点tricky的地方就是 需要O(1) 的space, 这样就利用第一个行 和第一列作为整个matrix的标记位,再用 row 和 col两个作为第一列和第一个行的标志位

class Solution:
    # @param {integer[][]} matrix
    # @return {void} Do not return anything, modify matrix in-place instead.
    def setZeroes(self, matrix):
        m = len(matrix)
        if m == 0:
            return matrix
        n = len(matrix[0])
        row, col = False, False
        for j in range(0 ,n):
            if matrix[0][j] == 0:
                row = True
                break
        for i in range(0,m):
            if matrix[i][0] == 0:
                col = True
                break
        for i in range(0, m):
            for j in range(0,n):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        for i in range(1, m):
            for j in range(1,n):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                    matrix[i][j] = 0
        if row:
            for j in range(0 ,n):
                matrix[0][j] = 0
        if col:
            for i in range(0,m):
                matrix[i][0] = 0
时间: 2024-10-21 18:31:57

073 Set Matrix Zeroes的相关文章

Java for LeetCode 073 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. 解题思路: 用两个boolean数组row col表示行列是否有零即可,JAVA实现如下: public void setZeroes(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; boolean[] row

LeetCode: Set Matrix Zeroes [073]

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. [题意] 给定一个mXn的矩阵,如果其中的元素为0,则对应的行和列都用0填充. 不能申请额外的空间. [思路] 第一行和第一列空出来标记需要置0的列和行 第一遍扫描: 扫描第一行,判断第一行是否需要清零 扫描第一列,判断第一列是否需要清零 扫描[1,1]~[m-1, n-1]的区块,如果某个位置上的值

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 impro

Set Matrix Zeroes

73 Set Matrix Zeroes 原文链接:http://blog.csdn.net/doc_sgl/article/details/11834933 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) s

【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 使用第一行和第一列记录某

Leetcode 细节实现 Set Matrix Zeroes

Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions 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. 题意:给定矩阵,如果矩阵的某个位置为0,则把那一行那一列的所有元素都置为0 思路:用两个bool数组,分

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

【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】

[070-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. 题目大意 给定一个m*n的矩阵,如果某个位置是0.将对应的行和列设置为0. 解题思路 先对矩阵进行扫描,标记要进行置0的行和列,对要进行置0的行在第0列上进行标记,对置0的列在