求問一個(gè)二維矩陣某個(gè)元素為其對應(yīng)行和列唯一一個(gè)1的元素個(gè)數(shù),用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