Sunday, March 10, 2013

[LeetCode] Rotate Image

Thought:
Rotate from the outside to the inside.

Code:
public class Solution {
    public void rotate(int[][] matrix) {
        int layer = matrix.length;
        for (int i = 0; i < layer / 2; i ++) {
            for (int j = i; j < layer - i - 1; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[layer - 1 - j][i];
                matrix[layer - 1 - j][i] = matrix[layer - 1 - i][layer - 1 - j];
                matrix[layer - 1 - i][layer - 1 - j] = matrix[j][layer - 1 - i];
                matrix[j][layer - 1 - i] = temp;
            }
        }
        return;
    }
}

No comments:

Post a Comment