Skip to content

Commit

Permalink
048 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Sep 13, 2018
1 parent 9842b3e commit 7b15635
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions 0048-Rotate-Image/cpp-0048/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
61 changes: 61 additions & 0 deletions 0048-Rotate-Image/cpp-0048/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Source : https://leetcode.com/problems/rotate-image/description/
/// Author : liuyubobobo
/// Time : 2018-09-13

#include <iostream>
#include <vector>

using namespace std;


/// Ad-Hoc
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class Solution {
public:
void rotate(vector<vector<int>>& 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<vector<int>>& 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<vector<int>> matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Solution().rotate(matrix1);
Solution::print_matrix(matrix1);

vector<vector<int>> 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;
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 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/) | |
Expand Down

0 comments on commit 7b15635

Please sign in to comment.