1. What I learned
a. Swap
If you want to exchange two numbers, write as follows.
a,b = b,a
b. method reverse()
‘LIST.reverse()’ returns a list of elements in reverse order.
2. How I sloved
I approached each row and used reverse() method. It reversed the order of all elements in the row. And if each element was 0, it was changed to 1, otherwise to 0.
3. Code
class Solution:
def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
for row in A:
row.reverse()
for i in range(0,len(A[0])):
row[i] ^= 1
return A
class Solution:
def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
for i in range(0, len(A)):
for j in range(0,int(len(A[0])/2)):
A[i][j], A[i][len(A[0])-1-j] = A[i][len(A[0])-1-j], A[i][j]
for i in range(0, len(A)):
for j in range(0,len(A[0])):
if A[i][j] == 0:
A[i][j] = 1
else:
A[i][j] = 032
return A
4. Result
Runtime : 52 ms(73.65%), Memory usage : 13.6 MB(96.41%)
(Runtime can be different by a system even if it is a same code.)