Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example 1:
Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ]
Example 2:
Input: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] Output: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ]
题意:把0所在行和所在列都变为0
思路:把0所在的坐标存起来,然后该行和列,都变为0
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
zero_set=set()
row=len(matrix)
column=len(matrix[0])
for i in range(row):
for j in range(column):
if(matrix[i][j]==0):
zero_set.add((i,j))
for i,j in zero_set:
for jj in range(column):
matrix[i][jj]=0
for ii in range(row):
matrix[ii][j]=0