美文网首页
这周一道算法题(五十五)

这周一道算法题(五十五)

作者: CrazySteven | 来源:发表于2018-06-10 22:49 被阅读18次

本周题目难度级别'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来说肯定有更优解,有空再优化吧。

版权声明:本文为 Crazy Steven 原创出品,欢迎转载,转载时请注明出处!

相关文章

网友评论

      本文标题:这周一道算法题(五十五)

      本文链接:https://www.haomeiwen.com/subject/olnqeftx.html