-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path6-sally0226.cpp
More file actions
35 lines (32 loc) · 820 Bytes
/
6-sally0226.cpp
File metadata and controls
35 lines (32 loc) · 820 Bytes
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
#include <vector>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
vector<string> zigzag(numRows,"");
int row = 0;
bool direction = 0; // 0:down(++), 1:up(--)
if (numRows == 1)
return s;
for (int i=0;i<s.size();i++){
zigzag[row].push_back(s[i]);
if (direction==0)
row++;
else
row--;
if (row == numRows){
row-=2;
direction = !direction;
}
if (row == -1){
row+=2;
direction = !direction;
}
}
string result="";
for (int i=0;i<numRows;i++){
result += zigzag[i];
}
return result;
}
};