leetcode(三)

leedcode—problem861

rank：medium

1.问题

2.解决方案

``` 1 class Solution:
2     def matrixScore(self, A):
3         """
4         :type A: List[List[int]]
5         :rtype: int
6         """
7         height = len(A)
8         width = len(A[0])
9
10         def toggle_row(A, row):
11             for j in range(width):
12                 if A[row][j]:
13                     A[row][j] = 0
14                 else:
15                     A[row][j] = 1
16             return A
17
18         def toggle_column(A, column):
19             for i in range(height):
20                 if A[i][column]:
21                     A[i][column] = 0
22                 else:
23                     A[i][column] = 1
24             return A
25
26         def count_column_one(A, column):
27             num = 0
28             for i in range(height):
29                 if A[i][column] == 1:
30                     num += 1
31             return num
32
33         for i in range(height):
34             if A[i][0] == 0:
35                 A = toggle_row(A, i)
36
37         for j in range(width-1):
38             num = count_column_one(A, j+1)
39             if num < height/2:
40                 A = toggle_column(A,j+1)
41         from functools import reduce
42         result = 0
43         for i in range(height):
44             result += reduce(lambda x,y: x*2+y, A[i])
45         return result```

53 篇文章14 人订阅

0 条评论

40880

83220

18010

3.4K40

29180

30230

27180

35450