LeetCode-5511-二进制矩阵中的特殊位置

5511. 二进制矩阵中的特殊位置

说明

给你一个大小为 rows x cols 的矩阵 mat,其中 mat[i][j] 是 0 或 1,请返回 矩阵 mat 中特殊位置的数目 。

特殊位置 定义:如果 mat[i][j] == 1 并且第 i 行和第 j 列中的所有其他元素均为 0(行和列的下标均 从 0 开始 ),则位置 (i, j) 被称为特殊位置。

示例

示例1
输入:mat = [[1,0,0],
            [0,0,1],
            [1,0,0]]
输出:1
解释:(1,2) 是一个特殊位置,因为 mat[1][2] == 1 且所处的行和列上所有其他元素都是 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/special-positions-in-a-binary-matrix
示例2
输入:mat = [[1,0,0],
            [0,1,0],
            [0,0,1]]
输出:3
解释:(0,0), (1,1) 和 (2,2) 都是特殊位置

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/special-positions-in-a-binary-matrix

编码能力较弱,使用的纯笨蛋方法,纯判断

代码实现

class Solution(object):
    def numSpecial(self, mat):
        """
        :type mat: List[List[int]]
        :rtype: int
        """
        count = 0
        rows = len(mat)
        for i in range(rows):
            cols = len(mat[i])
            for j in range(cols):
                if mat[i][j] == 1:
                    ans = True
                    for x in range(cols):
                        if x == j:
                            continue
                        elif mat[i][x] == 1:
                            ans = False
                            break
                    if ans:
                        for y in range(rows):
                            if y == i:
                                continue
                            elif mat[y][j] == 1:
                                ans = False
                                break
                    if ans:
                        count += 1              
        return count
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值