public void rotate(int[][] matrix) {
        int n = matrix.length;
        for(int layer = 0; layer < n/2; layer++){
            int first = layer;
            int last = n - layer - 1;
            for(int i = first; i < last; i++){
                int offset = i - first;
                int tmp = matrix[first][i];
                //left-bottom to left-top
                matrix[first][i] = matrix[last - offset][first];
                //right-bottom to left-bottom
                matrix[last - offset][first] = matrix[last][last - offset];
                //right-top to right-bottom
                matrix[last][last - offset] = matrix[i][last];
                //left-top to right-top
                matrix[i][last] = tmp;
            }
        }
    }