﻿ 在Java中旋转NxN矩阵 - 代码日志

#### 在Java中旋转NxN矩阵

Given an image represented by an NxN matrix, where each pixel in the
image is 4 bytes, write a method to rotate the image by 90 degrees.
Can you do this in place?

public static void rotate(int[][] matrix, int n) {
for (int layer = 0; layer < n / 2; ++layer) {
int first = layer;
int last = n - 1 - layer;
for(int i = first; i < last; ++i) {
int offset = i - first;
int top = matrix[first][i]; // save top

// left -> top
matrix[first][i] = matrix[last-offset][first];

// bottom -> left
matrix[last-offset][first] = matrix[last][last - offset];

// right -> bottom
matrix[last][last - offset] = matrix[i][last];

// top -> right
matrix[i][last] = top; // right <- saved top
}
}
}

ABCD
EFGH
IJKL
MNOP

ABCD
E  H
I  L
MNOP

int top = matrix[first][i]; // save top

// left -> top
matrix[first][i] = matrix[last-offset][first];

// bottom -> left
matrix[last-offset][first] = matrix[last][last - offset];

// right -> bottom
matrix[last][last - offset] = matrix[i][last];

// top -> right
matrix[i][last] = top; // right <- saved top

ABCD
EFGH
IJKL
MNOP

MIEA
NFGB
OJKC
PLHD

MIEA
NJFB
OKGC
PLHD
http://stackoverflow.com/questions/25882480/rotating-a-nxn-matrix-in-java