LeetCode #1582. Special Positions in a Binary Matrix
题目
1582. Special Positions in a Binary Matrix
解题方法
设置一个字典记录矩阵中1的位置,遍历字典中的键,判断纵向和横向上的坐标是否在字典中,且不是该点,如果都没有就把它记录为满足条件的点。
时间复杂度:O(mn)
空间复杂度:O(mn)
代码
class Solution:
def numSpecial(self, mat: List[List[int]]) -> int:
dic = {}
for i in range(len(mat)):
for j in range(len(mat[i])):
if mat[i][j]:
dic[(i,j)] = 1
rat = 0
for key in dic.keys():
colone = 0
for i in range(len(mat)):
if (i, key[1]) in dic and i != key[0]:
colone = 1
break
if colone: continue
rowone = 0
for j in range(len(mat[0])):
if (key[0], j) in dic and j != key[1]:
rowone = 1
break
if rowone:
pass
else:
rat += 1
return rat