problem 6

This commit is contained in:
Buduf 2022-04-14 13:35:39 +02:00
parent 8e567c7789
commit 39b4c5191e

27
ZigZagConversion.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <string>
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;
}
};