1582. 二进制矩阵中的特殊位置 第 206 场周赛

1582. 二进制矩阵中的特殊位置 第 206 场周赛

传送门

传送门

结题思路

# 思路1:首先计算每一行,每一列有几个1。再遍历矩阵的每一个数,若此数为1,则判断之前记录的第i行第j列是否都为1,若都是,则num += 1。
class Solution(object):
    def numSpecial(self, mat):
        """
        :type mat: List[List[int]]
        :rtype: int
        """
        num = 0
        row_1 = [0] * len(mat)
        col_1 = [0] * len(mat[0])
        for i in range(len(mat)):
            for j in range(len(mat[0])):
                row_1[i] += mat[i][j]
                col_1[j] += mat[i][j]
        for i in range(len(mat)):
            for j in range(len(mat[0])):
                if(mat[i][j] == 1 and row_1[i] == 1 and col_1[j] == 1):
                    num += 1
        return num

猜你喜欢

转载自blog.csdn.net/qq_40092110/article/details/108668441