-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path0048-rotate-image.swift
More file actions
28 lines (22 loc) · 887 Bytes
/
0048-rotate-image.swift
File metadata and controls
28 lines (22 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
func rotate(_ matrix: inout [[Int]]) {
var left = 0, right = matrix.count - 1
while left < right {
for i in 0...right-left-1 {
let top = left, bottom = right
// save the topLeft
let topLeft = matrix[top][left + i]
// move bottom left into top left
matrix[top][left + i] = matrix[bottom - i][left]
// move bottom right into bottom left
matrix[bottom - i][left] = matrix[bottom][right - i]
// move top right into bottom right
matrix[bottom][right - i] = matrix[top + i][right]
// move top left into top right
matrix[top + i][right] = topLeft
}
right -= 1
left += 1
}
}
}