Thursday, November 19, 2015

[leetcode] Rotate Image

要想仔细一点不要只想第一层
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Solution {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length <= 1 || matrix.length != matrix[0].length) return;
        int start = 0;
        int end = matrix.length-1;
        while (start < end){
            for (int i = 0; i < (end-start); i++){
                int temp = matrix[end-i][start];
                matrix[end-i][start] = matrix[end][end-i];
                matrix[end][end-i] = matrix[start+i][end];
                matrix[start+i][end] = matrix[start][start+i];
                matrix[start][start+i] = temp;
            }
            start++;
            end--;
        }
    }
}

No comments:

Post a Comment