题目链接: https://leetcode.cn/problems/rotate-image/
- 矩阵旋转时,原有的
(row,coll)
位置的元素经过旋转,会变换到(coll,n-row-1)
的位置 - 所以我们可以逐行遍历矩阵,将每行元素旋转复制到一个临时矩阵中,在把最终结果复制回原矩阵
func rotate(matrix [][]int) {
if matrix == nil || len(matrix) == 0 || len(matrix) == 1 {
return
}
n := len(matrix)
tmp := make([][]int, n)
for idx := range tmp {
tmp[idx] = make([]int, n)
}
for i, item := range matrix {
for j, cell := range item {
tmp[j][n-i-1] = cell
}
}
copy(matrix, tmp)
}
- 时间复杂度: 时间复杂度为$$O(n^2)$$,$$n$$为矩阵的行数
- 空间复杂度: 空间复杂度为$$O(n^2)$$,$$n$$为矩阵的行数