-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode-6-Zigzag_Conversion-V1.cpp
57 lines (47 loc) · 1.08 KB
/
leetcode-6-Zigzag_Conversion-V1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
std::string convert(std::string s, int numRows) {
std::vector<std::vector<char>> zig;
for(size_t i = 0; i<numRows; ++i){
std::vector<char> temp_row;
temp_row.resize(s.size(),'-');
zig.emplace_back(temp_row);
}
size_t col = 0,
i = 0;
while(i<s.size())
{
int row = 0;
while(i<s.size() && row<numRows)
{
zig[row++][col] = s[i++];
}
--row;
while(i<s.size() && 1<row && col<s.size())
{
zig[--row][++col] = s[i++];
}
++col;
}
std::string res;
for(size_t row = 0; row<numRows; ++row)
{
for(size_t col = 0; col<s.size(); ++col)
{
if(zig[row][col] != '-')
{
res += zig[row][col];
}
}
}
return res;
}
int main(){
std::string s = "PAYPALISHIRING";
int numRows = 3;
auto res = convert(s, numRows);
std::cout << res << std::endl;
return EXIT_SUCCESS;
}