From 5b0022c21a6c1f242696b36df55df7a1fad2e219 Mon Sep 17 00:00:00 2001 From: juan Date: Fri, 2 Sep 2022 16:12:45 +0800 Subject: [PATCH] vault backup: 2022-09-02 16:12:45 --- OJ notes/pages/Leetcode Rotate-Image.md | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/OJ notes/pages/Leetcode Rotate-Image.md b/OJ notes/pages/Leetcode Rotate-Image.md index 2203a59..4cf018b 100644 --- a/OJ notes/pages/Leetcode Rotate-Image.md +++ b/OJ notes/pages/Leetcode Rotate-Image.md @@ -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> &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> &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> &matrix) { + // transpose and reflect + + transpose(matrix); + reflect(matrix); + } +}; +``` \ No newline at end of file