-
-
Save NicolaM94/a5a04dc9a5de808565148ea7701c5b17 to your computer and use it in GitHub Desktop.
Algorithm to rotate a matrix by 90° (not in place rotation)
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
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