Skip to content

Instantly share code, notes, and snippets.

@vrat28
Last active April 25, 2021 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/2a2802898ce65887ec941a0abdbe5f5d to your computer and use it in GitHub Desktop.
Save vrat28/2a2802898ce65887ec941a0abdbe5f5d to your computer and use it in GitHub Desktop.
Rotate Image( Using Transpose and Reverse)
class Solution {
func rotate(_ matrix: inout [[Int]]) {
let n = matrix.count
// Transpose
for i in 0..<n {
for j in 0..<n {
if i <= j {
(matrix[i][j],matrix[j][i]) = (matrix[j][i] ,matrix[i][j])
}
}
}
// Reverse rows
for i in 0..<n {
var low = 0, high = n - 1
while low < high, low < n/2 {
( matrix[i][low], matrix[i][high] ) = (matrix[i][high], matrix[i][low])
low += 1
high -= 1
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment