给出一个0-1矩阵,输出一个一样大小的矩阵,其中每个元素代表原先矩阵对应行和列的1的个数减去0的个数,O(n^2)直接做
1 #2482
2 #Runtime: 1162 ms (Beats 74.29%)
3 #Memory: 44.6 MB (Beats 80%)
4
5 class Solution(object):
6 def onesMinusZeros(self, grid):
7 """
8 :type grid: List[List[int]]
9 :rtype: List[List[int]]
10 """
11 n, m = len(grid), len(grid[0])
12 r = [0] * n
13 c = [0] * m
14 for i in xrange(n):
15 for j in xrange(m):
16 r[i] += grid[i][j]
17 c[j] += grid[i][j]
18 for i in xrange(n):
19 for j in xrange(m):
20 grid[i][j] = 2 * r[i] + 2 * c[j] - n - m
21 return grid