Created
April 12, 2020 05:53
-
-
Save Skarlso/d3030275d7c844fb1f255b612551a130 to your computer and use it in GitHub Desktop.
Rotate a matrix 90 degrees
This file contains hidden or 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
// going with the transpose / reverse solution | |
func rotate(matrix [][]int) { | |
n := len(matrix); | |
for i := 0; i<n; i++ { | |
for j := i + 1; j < n; j++ { | |
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] | |
} | |
} | |
for i := 0; i<n; i++ { | |
reverse(matrix[i]) | |
} | |
} | |
func reverse (a []int) { | |
for left, right := 0, len(a)-1; left < right; left, right = left+1, right-1 { | |
a[left], a[right] = a[right], a[left] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment