Skip to content

Commit

Permalink
Create: 48-Rotate-Image.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 2, 2022
1 parent 5e85b7e commit ef9b2aa
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions swift/48-Rotate-Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}
}

0 comments on commit ef9b2aa

Please sign in to comment.