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