原题地址:https://oj.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.
解题思路:分别记录两个向量x, y,保存行和列是否有0,再次遍历数组时查询对应的行和列然后修改值。
代码:
class Solution: # @param matrix, a list of lists of integers # RETURN NOTHING, MODIFY matrix IN PLACE. def setZeroes(self, matrix): rownum = len(matrix) colnum = len(matrix[0]) row = [False for i in range(rownum)] col = [False for i in range(colnum)] for i in range(rownum): for j in range(colnum): if matrix[i][j] == 0: row[i] = True col[j] = True for i in range(rownum): for j in range(colnum): if row[i] or col[j]: matrix[i][j] = 0