[Leetcode]@python 73. Set Matrix Zeroes

题目链接

https://leetcode.com/problems/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.

题目大意

给出m x n的矩阵,若该矩阵上某个元素为0,则将该元素所处的列和行都置零

解题思路

分别记录两个向量x, y,保存行和列是否有0,再次遍历数组时查询对应的行和列然后修改值。

代码

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        rowLen = len(matrix)
        colLen = len(matrix[0])

        row = [False for i in range(rowLen)]
        col = [False for i in range(colLen)]

        for i in range(rowLen):
            for j in range(colLen):
                if matrix[i][j] == 0:
                    row[i] = True
                    col[j] = True

        for i in range(rowLen):
            for j in range(colLen):
                if row[i] or col[j]:
                    matrix[i][j] = 0   
posted @ 2016-01-14 17:22  slurm  阅读(193)  评论(0编辑  收藏  举报