列表

详情


73. 矩阵置零

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法

 

示例 1:

输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:[[1,0,1],[0,0,0],[1,0,1]]

示例 2:

输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

 

提示:

 

进阶:

相似题目

生命游戏

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: void setZeroes(vector<vector<int>>& matrix) { } };

golang 解法, 执行用时: 12 ms, 内存消耗: 6.2 MB, 提交时间: 2021-07-17 10:23:36

func setZeroes(matrix [][]int)  {
    row := make([]bool, len(matrix))
    col := make([]bool, len(matrix[0]))
    for i, r := range matrix {
        for j, v := range r {
            if v == 0 {
                row[i] = true
                col[j] = true
            }
        }
    }

    for i, r := range matrix {
        for j := range r {
            if row[i] || col[j] {
                r[j] = 0
            }
        }
    }
}

golang 解法, 执行用时: 16 ms, 内存消耗: 6.2 MB, 提交时间: 2021-07-17 10:18:46

func setZeroes(matrix [][]int)  {
    m, n := len(matrix), len(matrix[0])
    is_col := false   // (0, 0)标记
    for i := 0; i < m; i++ {
        if matrix[i][0] == 0 {
            is_col = true
        }
        for j := 1; j < n; j++ {
            if matrix[i][j] == 0 {
                matrix[i][0] = 0
                matrix[0][j] = 0
            }
        }
    }
        
    for 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 matrix[0][0] == 0 {
        for j := 0; j < n; j++ {
            matrix[0][j] = 0
        }
    }
        
    if is_col == true {
        for i := 0; i < m; i++ {
            matrix[i][0] = 0
        }
    }
}

python3 解法, 执行用时: 52 ms, 内存消耗: 13.8 MB, 提交时间: 2020-11-18 16:16:57

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m, n = len(matrix), len(matrix[0])
        is_col = False   # (0, 0)标记
        for i in range(m):
            if matrix[i][0]  == 0:
                is_col = True
            for j in range(1, 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 matrix[0][0] == 0:
            for j in range(n):
                matrix[0][j] = 0
        
        if is_col:
            for i in range(0, m):
                matrix[i][0] = 0

python3 解法, 执行用时: 40 ms, 内存消耗: 13.9 MB, 提交时间: 2020-11-18 14:35:01

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m, n = len(matrix), len(matrix[0])
        set_m, set_n = set(), set()
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == 0:
                    set_m.add(i)
                    set_n.add(j)
        
        for i in range(m):
            for j in range(n):
                if i in set_m or j in set_n:
                    matrix[i][j] = 0

        

上一题