forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (34 loc) · 922 Bytes
/
Solution.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
// @ID:6. ZigZag Conversion
// @author:jxdeng3989
class Solution {
public:
string convert(string s, int numRows) {
string retstr;
if(1==numRows)
return s;
for(int i=0; i<numRows; ++i)
{
retstr.push_back(s[i]);
int maxspan = 2*(numRows-1);
int span1 = maxspan-i*2;
int span2 = maxspan - span1;
int cntpos = i;
if(span1==0)
span1 = span2;
if(span2==0)
span2 = span1;
while(1)
{
if(cntpos+span1>=s.size())
break;
cntpos += span1;
retstr.push_back(s[cntpos]);
if(cntpos+span2>=s.size())
break;
cntpos += span2;
retstr.push_back(s[cntpos]);
}
}
return retstr;
}
};