vault backup: 2022-09-02 16:12:45

This commit is contained in:
juan 2022-09-02 16:12:45 +08:00
parent 6624018d9e
commit 5b0022c21a

View file

@ -54,7 +54,43 @@ Mainly two ways
#### Method 1: rotate in rings
#TODO complete in this method.
#### Method 2: transpose and reflect
transpose reflects the matrix in `y = x`,
reflect changes matrix in axis y
### Solution
```cpp
class Solution {
void reflect(vector<vector<int>> &mat) {
// reflect in y axis.
int n = mat.size();
for (int i = 0; i < n; i++) {
for (int j = 0, half = n / 2; j < half; j++) {
swap(mat[i][j], mat[i][n - j - 1]);
}
}
}
void transpose(vector<vector<int>> &mat) {
int n = mat.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
swap(mat[i][j], mat[j][i]);
}
}
}
public:
void rotate(vector<vector<int>> &matrix) {
// transpose and reflect
transpose(matrix);
reflect(matrix);
}
};
```