Last active
April 25, 2021 15:21
-
-
Save vrat28/2a2802898ce65887ec941a0abdbe5f5d to your computer and use it in GitHub Desktop.
Rotate Image( Using Transpose and Reverse)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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