From c738e8abfaa71fc3f414256d88af9c2e87ac4b15 Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Thu, 12 Oct 2023 09:28:53 -0700 Subject: [PATCH] day 14: 48-rotate-image checked soln for this one, so doesn't actly count --- 48-rotate-image/driver.cpp | 10 ++++++++++ 48-rotate-image/soln.cpp | 24 ++++++++++++++++++++++++ 48-rotate-image/soln.hpp | 8 ++++++++ 3 files changed, 42 insertions(+) create mode 100644 48-rotate-image/driver.cpp create mode 100644 48-rotate-image/soln.cpp create mode 100644 48-rotate-image/soln.hpp diff --git a/48-rotate-image/driver.cpp b/48-rotate-image/driver.cpp new file mode 100644 index 0000000..ff17364 --- /dev/null +++ b/48-rotate-image/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector> matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + vector> answer{{7, 4, 1}, {8, 5, 2}, {9, 6, 3}}; + Solution soln; + cout << "Rotated image matrix correctly? " << soln.test(matrix, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/48-rotate-image/soln.cpp b/48-rotate-image/soln.cpp new file mode 100644 index 0000000..056f685 --- /dev/null +++ b/48-rotate-image/soln.cpp @@ -0,0 +1,24 @@ +#include "soln.hpp" + +void Solution::rotate(vector> &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> &matrix, vector> &answer) +{ + vector> mat_copy(matrix); + rotate(mat_copy); + return mat_copy == answer; +} \ No newline at end of file diff --git a/48-rotate-image/soln.hpp b/48-rotate-image/soln.hpp new file mode 100644 index 0000000..eef31e7 --- /dev/null +++ b/48-rotate-image/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + void rotate(vector> &matrix); + bool test(vector> &matrix, vector> &answer); +}; \ No newline at end of file