day 14: 48-rotate-image

checked soln for this one, so doesn't actly count
This commit is contained in:
Kaushik Narayan R 2023-10-12 09:28:53 -07:00
parent 26a6ac63b0
commit c738e8abfa
3 changed files with 42 additions and 0 deletions

View 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
View 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
View 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);
};