题意: 给出 n * n的矩阵,要求将矩阵顺时针旋转90°(不使用额外空间)
1 /*
2 Basically, divide the array into 4 along the diagonals,
3 then for each element in the top quadrant, place it into
4 the slot 90 degrees cw, and the old 90 in 180 degrees cw,
5 and the old 180 in 270 degrees , and the old 270 in
6 the original place.
7 */
8 class Solution {
9 public:
10 void rotate(vector<vector<int> > &matrix) {
11 int n = (int)matrix.size();
12 for (int i = 0; i < n/2; i++) for (int j = i; j < n-1-i; j++) {
13 swap(matrix[i][j], matrix[j][n-1-i]);
14 swap(matrix[i][j], matrix[n-1-i][n-1-j]);
15 swap(matrix[i][j], matrix[n-1-j][i]);
16 }
17 }
18 };