mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 05:24:07 +00:00
day 14: 48-rotate-image
checked soln for this one, so doesn't actly count
This commit is contained in:
parent
26a6ac63b0
commit
c738e8abfa
10
48-rotate-image/driver.cpp
Normal file
10
48-rotate-image/driver.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<vector<int>> matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
vector<vector<int>> answer{{7, 4, 1}, {8, 5, 2}, {9, 6, 3}};
|
||||
Solution soln;
|
||||
cout << "Rotated image matrix correctly? " << soln.test(matrix, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
24
48-rotate-image/soln.cpp
Normal file
24
48-rotate-image/soln.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
void Solution::rotate(vector<vector<int>> &matrix)
|
||||
{
|
||||
// take your phone
|
||||
// flip it top over bottom
|
||||
reverse(matrix.begin(), matrix.end());
|
||||
// then flip it across its leading diagonal
|
||||
// voila, 90 degree rotation!
|
||||
for (int i = 0; i < matrix.size(); i++)
|
||||
{
|
||||
for (int j = i; j < matrix[0].size(); j++)
|
||||
{
|
||||
swap(matrix[i][j], matrix[j][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Solution::test(vector<vector<int>> &matrix, vector<vector<int>> &answer)
|
||||
{
|
||||
vector<vector<int>> mat_copy(matrix);
|
||||
rotate(mat_copy);
|
||||
return mat_copy == answer;
|
||||
}
|
||||
8
48-rotate-image/soln.hpp
Normal file
8
48-rotate-image/soln.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
void rotate(vector<vector<int>> &matrix);
|
||||
bool test(vector<vector<int>> &matrix, vector<vector<int>> &answer);
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user