Skip to content

Instantly share code, notes, and snippets.

@jamak
Created December 12, 2012 23:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamak/4272680 to your computer and use it in GitHub Desktop.
Save jamak/4272680 to your computer and use it in GitHub Desktop.
def rotate(matrix, n):
for layer in xrange(n/2):
first = layer
last = n - 1 - layer
for i in xrange(first, last):
offset = i - first
top = matrix[first][i]
# (0, 0) goes to (N-1, 0)
matrix[first][i] = matrix[last-offset][first]
# (0, N-1) goes to (0, 0)
matrix[last-offset][first] = matrix[last][last - offset]
# (0, N-1) goes to (N-1, N-1)
matrix[last][last - offset] = matrix[i][last]
# (N-1, N-1) goes to (0, N-1)
matrix[i][last] = top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment