73 leetcode - Set Matrix Zeroes

来源:互联网 发布:算法基础第五版答案 编辑:程序博客网 时间:2024/05/29 05:57
#!/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] = 0if __name__ == "__main__":    s = Solution()    m = [[1,2],[1,2]]    s.setZeroes(m)    print m
0 0
原创粉丝点击