#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
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.
'''
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
row_len = len(matrix)
if row_len == 0:
return
col_len = len(matrix[0])
if col_len == 0:
return
in_place = []
for row,row_val in enumerate(matrix):
for col,val in enumerate(row_val):
if val == 0:
in_place.append((row,col))
for i in in_place:
for index in range(row_len):
matrix[index][i[1]] = 0
for index in range(col_len):
matrix[i[0]][index] = 0
if __name__ == "__main__":
s = Solution()
m = [[1,2],[1,2]]
s.setZeroes(m)
print m
73 leetcode - Set Matrix Zeroes
最新推荐文章于 2023-06-13 17:19:00 发布