diff --git a/0048-Rotate-Image/cpp-0048/CMakeLists.txt b/0048-Rotate-Image/cpp-0048/CMakeLists.txt new file mode 100644 index 00000000..f8ab3568 --- /dev/null +++ b/0048-Rotate-Image/cpp-0048/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.5) +project(cpp_0048) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") + +set(SOURCE_FILES main.cpp) +add_executable(cpp_0048 ${SOURCE_FILES}) \ No newline at end of file diff --git a/0048-Rotate-Image/cpp-0048/main.cpp b/0048-Rotate-Image/cpp-0048/main.cpp new file mode 100644 index 00000000..77e7ceab --- /dev/null +++ b/0048-Rotate-Image/cpp-0048/main.cpp @@ -0,0 +1,61 @@ +/// Source : https://leetcode.com/problems/rotate-image/description/ +/// Author : liuyubobobo +/// Time : 2018-09-13 + +#include +#include + +using namespace std; + + +/// Ad-Hoc +/// Time Complexity: O(n^2) +/// Space Complexity: O(1) +class Solution { +public: + void rotate(vector>& matrix) { + + int n = matrix.size(); + for(int i = 0; i <= n / 2; i ++) + for(int j = i; j < n - 1 - i; j ++){ + int t = matrix[i][j]; + matrix[i][j] = matrix[n - 1 - j][i]; + matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j]; + matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i]; + matrix[j][n - 1 - i] = t; + Solution::print_matrix(matrix); + } + } + + static void print_matrix(const vector>& m){ + for(int i = 0; i < m.size(); i ++){ + for(int j = 0; j < m[i].size(); j ++) + cout << m[i][j] << " "; + cout << endl; + } + cout << endl; + } +}; + + +int main() { + + vector> matrix1 = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + Solution().rotate(matrix1); + Solution::print_matrix(matrix1); + + vector> matrix2 = { + {1, 2, 3, 4}, + {5, 6, 7, 8}, + {9, 10, 11, 12}, + {13, 14, 15, 16} + }; + Solution().rotate(matrix2); + Solution::print_matrix(matrix2); + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index c7ff27e5..092aab5a 100644 --- a/readme.md +++ b/readme.md @@ -82,7 +82,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 046 | [Permutations](https://leetcode.com/problems/permutations/description/) | [缺:排列算法整理] | [C++](0046-Permutations/cpp-0046/) | [Java](0046-Permutations/java-0046/src/) | | | 047 | [Permutations II](https://leetcode.com/problems/permutations-ii/description/) | [无] | [C++](0047-Permutations-II/cpp-0047/) | | | -| | | | | | | +| 048 | [Rotate Image](https://leetcode.com/problems/rotate-image/description/) | [无] | [C++](0048-Rotate-Image/cpp-0048/) | | | | 049 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [solution](https://leetcode.com/problems/group-anagrams/solution/) | [C++](0049-Group-Anagrams/cpp-0049/) | | | | | | | | | | | 051 | [N-Queens](https://leetcode.com/problems/n-queens/description/) | [缺:N皇后问题整理] | [C++](0051-N-Queens/cpp-0051/) | [Java](0051-N-Queens/java-0051/src/) | |