-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path015Flipping_an_Image.py
More file actions
38 lines (34 loc) · 1.01 KB
/
Copy path015Flipping_an_Image.py
File metadata and controls
38 lines (34 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# encoding:utf-8
#
# Algorithm function:
# Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
# Input:
# [[1,1,0],[1,0,1],[0,0,0]]
# Output:
# [[1,0,0],[0,1,0],[1,1,1]]
#
class Solution(object):
def flipAndInvertImage(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
#reverse each list
row=len(A) #获取当前矩阵中列表个数
col=len(A[0]) #获取第一个列表中的列表元素个数
B=[]
c=[]
for i in range(row):
B=A[i]
B.reverse() #反转列表元素
c.append(B)
for i in range(row):
for j in range(col):
if(c[i][j]==1):
c[i][j]=0
else:
c[i][j]=1
return c
if __name__ == '__main__':
a = Solution().flipAndInvertImage([[1,0,1],[0,1,1],[0,1,1]])
print(a)