解题思路:找到原始点、最终点和中心点之间的关系,以3*3的矩阵为例
| 原始点 | 最终点 | 中心点 | 原始点-中心点 | 最终点-中心点 |
| (1,0) | (0,1) | (1,1) | (0,-1) | (-1,0) |
| (2,0) | (0,0) | (1,1) | (1,-1) | (-1,-1) |
| (0,1) | (1,2) | (1,1) | (-1,0) | (0,1) |
| (2,2) | (2,0) | (1,1) | (1,1) | (1,-1) |
然后就发现第四列的y和第五列的x相等,第四列的x和第五列的y互为相反数,在4*4的矩阵中验证了一下这个规律也是对的,就这么写了
1 class Solution(object): 2 def rotate(self, matrix): 3 """ 4 :type matrix: List[List[int]] 5 :rtype: None Do not return anything, modify matrix in-place instead. 6 """ 7 m = len(matrix) 8 n = len(matrix[0]) 9 help_m = [[0] * n for _ in range(m)] 10 i = 0 11 j = 0 12 c_m = (m - 1) / 2.0 13 c_n = (n - 1) / 2.0 14 while i < m: 15 j = 0 16 while j < n: 17 help_m[i][j] = matrix[int(c_m + c_n - j)][int(c_n - c_m + i)] 18 j += 1 19 i += 1 20 matrix[:] = help_m 21