解题思路:需使用constant-space,因此不能根据matrix大小额外开辟空间。所以使用第0行,第0列来记录。并记录其中是否有0,若有0则最后将0行0列全部置0。
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
if matrix == []:
return
column = False
row = False
m = len(matrix)
n = len(matrix[0])
for i in range(m):
if matrix[i][0] == 0:
row = True
break
for j in range(n):
if matrix[0][j] == 0:
column = True
break
for i in range(1,m):
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):
if matrix[i][0] == 0:
for j in range(1,n):
matrix[i][j] = 0
for j in range(1,n):
if matrix[0][j] == 0:
for i in range(1,m):
matrix[i][j] = 0
if column:
for j in range(n):
matrix[0][j] = 0
if row:
for i in range(m):
matrix[i][0] = 0