本周题目难度级别'Medium',使用语言'Python'
题目:给你一个二维数组,将数组内为0的行列都置为0。
思路:我的思路比较low,就是先遍历一遍,记录为0的行和列,然后再遍历一遍把记录的行和列置为0.下面看代码
class Solution:
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
row = []
col = []
rowCount = len(matrix)
colCount = len(matrix[0])
# 遍历记录行和列
for i in range(rowCount):
for j in range(colCount):
if (matrix[i][j] == 0):
row.append(i)
col.append(j)
# 遍历置为0
for i in range(rowCount):
for j in range(colCount):
if (i in row or j in col):
matrix[i][j] = 0
效率比较低,由其对于Python来说肯定有更优解,有空再优化吧。
网友评论