Skip to content

Instantly share code, notes, and snippets.

@NicolaM94
Last active October 1, 2023 19:29
Show Gist options
  • Save NicolaM94/a5a04dc9a5de808565148ea7701c5b17 to your computer and use it in GitHub Desktop.
Save NicolaM94/a5a04dc9a5de808565148ea7701c5b17 to your computer and use it in GitHub Desktop.
Algorithm to rotate a matrix by 90° (not in place rotation)
func RotateClockWiseNIP(matrix [][]int) (out [][]int) {
// Create and populate a new out matrix full of zero
for i := 0; i < len(matrix); i++ {
out = append(out, []int{})
for j := 0; j < len(matrix); j++ {
out[i] = append(out[i], 0)
}
}
// Change values accordingly
for row := 0; row < len(matrix); row++ {
for col := 0; col < len(matrix); col++ {
out[col][len(matrix)-1-row] = matrix[row][col]
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment