problem 6
This commit is contained in:
parent
8e567c7789
commit
39b4c5191e
27
ZigZagConversion.cpp
Normal file
27
ZigZagConversion.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user