vault backup: 2022-09-02 16:12:45
This commit is contained in:
parent
6624018d9e
commit
5b0022c21a
|
@ -54,7 +54,43 @@ Mainly two ways
|
||||||
|
|
||||||
#### Method 1: rotate in rings
|
#### Method 1: rotate in rings
|
||||||
|
|
||||||
|
#TODO complete in this method.
|
||||||
|
|
||||||
#### Method 2: transpose and reflect
|
#### Method 2: transpose and reflect
|
||||||
|
|
||||||
|
transpose reflects the matrix in `y = x`,
|
||||||
|
reflect changes matrix in axis y
|
||||||
|
|
||||||
### Solution
|
### 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
Loading…
Reference in a new issue