0%

面试题 01.07. Rotate Matrix LCCI

框架

1
2
3
4
5
6
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {

}
};

1. 先转置后对称

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
swap(matrix[i][j], matrix[j][i]);

for (int i = 0; i < n; i++)
for (int j = 0; j <= (n - 1) / 2; j++)
swap(matrix[i][j], matrix[i][n - 1 - j]);
}
};