[LeetCode]1582. Special Positions in a Binary Matrix (Easy) Python-2023.12.13
Posted on 2023-12-13 19:24 Uriel 閱讀(42) 評論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題求問一個二維矩陣某個元素為其對應行和列唯一一個1的元素個數,用python的count很方便
1 #1582
2 #Runtime: 121 ms (Beats 70.97%)
3 #Memory: 13.6 MB (Beats 35.48%)
4
5 class Solution(object):
6 def numSpecial(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in xrange(len(mat)):
13 if mat[i].count(1) == 1:
14 x = mat[i].index(1)
15 col = [r[x] for r in mat]
16 if col.count(1) == 1:
17 ans += 1
18 return ans
2 #Runtime: 121 ms (Beats 70.97%)
3 #Memory: 13.6 MB (Beats 35.48%)
4
5 class Solution(object):
6 def numSpecial(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in xrange(len(mat)):
13 if mat[i].count(1) == 1:
14 x = mat[i].index(1)
15 col = [r[x] for r in mat]
16 if col.count(1) == 1:
17 ans += 1
18 return ans