https://leetcode-cn.com/problems/set-matrix-zeroes/
# -*- coding:utf-8 -*- class Solution(object): def setZeroes(self, matrix): """ :type matrix: List[List[int]] :rtype: None Do not return anything, modify matrix in-place instead. """ x_list = [] y_list = [] # 首先检查0元素 for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j]==0: x_list.append(i) y_list.append(j) # 更改 for i in range(len(matrix)): for j in range(len(matrix[0])): if i in x_list or j in y_list: matrix[i][j] = 0 return matrix if __name__ == '__main__': matrix = [ [1, 1, 1], [1, 0, 1], [1, 1, 1] ] print(Solution().setZeroes(matrix))
思路:首先检查0元素位置,记录所在的行列,然后再次遍历,对出现在所在行所在列中的元素赋值为0.
0 条评论