-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.zigzag.cpp
40 lines (37 loc) · 915 Bytes
/
6.zigzag.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
//coding:utf-8
/*
Program: zigzag
Description:
Shanbo Cheng: [email protected]
Date: 2016-07-19 08:25:26
Last modified: 2016-08-18 15:23:31
GCC version: 4.7.3
*/
#include <string>
class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1)
return s;
string ret = "";
for(int i = 0; i < numRows; ++i) {
int first = (numRows - i - 1) * 2, second = i * 2;
int index = i, flag = 0;
while(index < s.length()) {
ret += s[index];
if(first == 0 || second == 0)
index += (numRows - 1) * 2;
else if(flag % 2 == 0){
flag++;
index += first;
} else {
flag++;
index += second;
}
}
}
return ret;
}
};
int main() {
}