给定一幅由N × N矩阵表示的图像,其中每个像素的大小为4字节,编写一种方法,将图像旋转90度。
不占用额外内存空间能否做到?
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋转输入矩阵,使其变为:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-matrix-lcci
matrix = [
[1, 2, 3],
[1, 22, 3]
]
matrix = [
[1, 2, 3]
]
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix = [[5, 1, 9, 11],
[2, 4, 8, 10],
[13, 3, 6, 7],
[15, 14, 12, 16]
]
R, C = len(matrix), len(matrix[0])
'''
123
456
789
741
852
963
顺时针旋转1/4圆
在纸上画下
------->
|
|
/
(x,y)-->(y,C-1-x) 坐标从0开始计数
x+y>0右上
(i,j)-->(C-1-i,R-1-j)
'''
# (x,y)-->(y,C+1-x) 坐标从1开始计数
# 不要求行数=列数
new = [[None for i in range(R)] for i in range(C)]
for x in range(R):
for y in range(C):
print('x,y', x, ',', y)
new[y][R - 1 - x] = matrix[x][y]