求问一个二维矩阵某个元素为其对应行和列唯一一个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