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.

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

cc150的题目,但是这里要求O(1)空间.

解法1自带魔法的python:

这题python因为可以不限定类别,所以可以作弊,先用别的字符'#'替代需要替换的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 not matrix or not matrix[0]:
            return 
        m = len(matrix)
        n = len(matrix[0])
        
        for i in xrange(m):
            for j in xrange(n):
                if matrix[i][j] == 0:
                    for k in xrange(m):
                        if matrix[k][j] != 0:
                            matrix[k][j] = '#'
                    for k in xrange(n):
                        if matrix[i][k] != 0:
                            matrix[i][k] = '#'
        for i in xrange(m):
            for j in xrange(n):
                if matrix[i][j] == '#':
                    matrix[i][j] = 0
        return 

用第一行,第一列存储行列是否为0的信息.

对于原本用O(m+n)空间保存每行每列是否置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 not matrix or not matrix[0]:
            return 
        m = len(matrix)
        n = len(matrix[0])
        
        fc = False #whether first column contains 0
        for i in xrange(m):  # the first column will be used to stored whether 0 exist in a row, so we must find whether 0 exist in the first column in advance. 
            if not matrix[i][0]:
                fc = True
                break
        for i in xrange(m):   #every row
            if 0 in matrix[i]:
                matrix[i][0] = 0
        for j in xrange(1,n): #every column except the first one
            if 0 in [matrix[i][j] for i in xrange(m)]:
                matrix[0][j] = 0
                
        for j in xrange(1,n): #every column except the first one
            if not matrix[0][j]:
                for i in xrange(1,m):
                    matrix[i][j] = 0
                    
        for i in xrange(m):  #set every row except the fi
            if not matrix[i][0]:
                for j in xrange(1,n):
                    matrix[i][j] = 0
       
        if fc:
            for i in xrange(m):
                matrix[i][0] = 0
        
        return

简化写法,简直机智:

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        if not matrix or not matrix[0]:
            return 
        m = len(matrix)
        n = len(matrix[0])
        fc = -1
        for i in xrange(m):
            if not matrix[i][0]: fc = 0
            for j in xrange(1,n):
                if not matrix[i][j]:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        for i in xrange(m-1,-1,-1):
            for j in xrange(n-1,0,-1): #first colunm is trackled beside.
               if (not matrix[i][0]) or (not matrix[0][j]):
                   matrix[i][j] = 0
            if not fc: matrix[i][0] = 0 #tackle first colunm
        return 
               

 

转载于:https://www.cnblogs.com/sherylwang/p/5919113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值