原题
https://leetcode.cn/problems/rotate-image/description/
思路
遍历
复杂度
时间:O(n * n)
空间:O(n * n)
Python代码
import copy
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
temp = copy.deepcopy(matrix)
for i in range(n):
# 当前行
row = temp[i]
# 旋转后所在的列
j = n - 1 - i
for k in range(n):
matrix[k][j] = row[k]
Go代码
func rotate(matrix [][]int) {
n := len(matrix)
// 拷贝数组
temp := make([][]int, n)
for i := range matrix {
temp[i] = make([]int, len(matrix[i]))
copy(temp[i], matrix[i])
}
for i := 0; i < n; i++ {
// 当前行
row := temp[i]
// 旋转后所在的列
j := n - 1 - i
for k := 0; k < n; k++ {
matrix[k][j] = row[k]
}
}
}
旋转图像问题解析与实现

被折叠的 条评论
为什么被折叠?



