>>> a
[[1, 2], [3, 4]]
>>> a[::-1]
[[3, 4], [1, 2]]
>>> b = []
>>> b[:] = zip(*a)
>>> b
[(1, 3), (2, 4)]
>>> b[:] = zip(*a[::-1])
>>> b
[(3, 1), (4, 2)]
>>>
>>> a = [1,2,3,4]
>>> a[-1]
4
>>> a[~1]
3
>>> a[~2]
2
>>>
其实就相当于a[~i] = a[len(a)-1-i]
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
matrix[:] = zip(*matrix[::-1])
class Solution:
def rotate(self, A):
n = len(A)
for i in range(n/2):
for j in range(n-n/2):
A[i][j], A[~j][i], A[~i][~j], A[j][~i] = \
A[~j][i], A[~i][~j], A[j][~i], A[i][j]
class Solution:
def rotate(self, A):
A[:] = map(list, zip(*A[::-1]))
class Solution:
def rotate(self, A):
A[:] = [[row[i] for row in A[::-1]] for i in range(len(A))]
class Solution:
def rotate(self, A):
n = len(A)
for i in range(n/2):
for j in range(n-n/2):
for _ in '123':
A[i][j], A[~j][i], i, j = A[~j][i], A[i][j], ~j, ~i
i = ~j
class Solution:
def rotate(self, A):
A.reverse()
for i in range(len(A)):
for j in range(i):
A[i][j], A[j][i] = A[j][i], A[i][j]
class Solution:
def rotate(self, A):
n = len(A)
for i in range(n):
for j in range(i):
A[i][j], A[j][i] = A[j][i], A[i][j]
for row in A:
for j in range(n/2):
row[j], row[~j] = row[~j], row[j]