美文网首页Leetcode
Leetcode 1253. Reconstruct a 2-R

Leetcode 1253. Reconstruct a 2-R

作者: SnailTyan | 来源:发表于2021-07-27 09:10 被阅读0次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Reconstruct a 2-Row Binary Matrix

    2. Solution

    解析:Version 1,由于是二值矩阵且只有两行,因此根据列的和来设置两行的值。

    • Version 1
    class Solution:
        def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]:
            n = len(colsum)
            matrix = [[0] * n, [0] * n]
            for i in range(n):
                if colsum[i] == 2:
                    matrix[0][i] = 1
                    matrix[1][i] = 1
                    upper -= 1
                    lower -= 1
                elif colsum[i] == 1:
                    if upper > lower:
                        matrix[0][i] = 1
                        upper -=1
                    else:
                        matrix[1][i] = 1
                        lower -=1
    
            if upper == 0 and lower == 0:
                return matrix
            return []
    

    Reference

    1. https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/

    相关文章

      网友评论

        本文标题:Leetcode 1253. Reconstruct a 2-R

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