From 39b4c5191e397566ac15f9eb5303305dbca3c080 Mon Sep 17 00:00:00 2001 From: Buduf Date: Thu, 14 Apr 2022 13:35:39 +0200 Subject: [PATCH] problem 6 --- ZigZagConversion.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ZigZagConversion.cpp diff --git a/ZigZagConversion.cpp b/ZigZagConversion.cpp new file mode 100644 index 0000000..c419df3 --- /dev/null +++ b/ZigZagConversion.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; +class Solution { +public: + string convert(string s, int numRows) { + if (numRows == 1) return s; + string* zigzag{ new string[numRows]{ } }; + int n{ }; + bool down{ }; + for (int i = 0; i < s.length(); i++) + { + zigzag[n] += s[i]; + if (n == numRows - 1|| n == 0) { + down = !down; + } + n += down ? 1 : -1; + } + string result{ }; + for (int i = 0; i < numRows; i++) + { + result += zigzag[i]; + } + delete[] zigzag; + return result; + } +}; \ No newline at end of file