48. 旋转图像

题目地址:https://leetcode-cn.com/problems/rotate-image/

执行用时:4 ms, 在所有 Swift 提交中击败了100.00%的用户
内存消耗:13.7 MB, 在所有 Swift 提交中击败了32.10%的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
func rotate(_ matrix: inout [[Int]]) {
let n = matrix.count
for i in 0 ..< n {
matrix[i].reverse()
}
var left = 1, bottom = 0
for i in stride(from: n-1, to: 0, by: -1) {
//向上一层
bottom = i - 1
for j in left ..< n {
let tmp = matrix[i][j]
matrix[i][j] = matrix[bottom][left-1]
matrix[bottom][left-1] = tmp
bottom -= 1
}
//向右一列
left += 1
}
}
}

48. 旋转图像
https://pisces34.github.io/2021/10/10/leetcode/48/
发布于
2021年10月10日
许可协议