Array
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.
即将矩阵中0元素所在的行和列的所有元素都换为0
example:
Input:
Output:
Solutions:
把0元素的行和列分别存起来,遍历完所有元素后,将存起来的行、列元素置为0
class Solution:
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
rows = []
colums = []
for i,row in enumerate(matrix):
if 0 in row:
rows.append(i)
for k in range(len(row)):
if row[k]==0 and k not in colums:
colums.append(k)
for i in rows:
for j in range(len(matrix[0])):
matrix[i][j]=0
for i in colums:
for j in range(len(matrix)):
matrix[j][i]=0
网友评论