Skip to content

Commit a68cf97

Browse files
authored
Merge pull request #33 from jxdeng3989/master
zig zag cpp code
2 parents f4b8abd + f90a731 commit a68cf97

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @ID:6. ZigZag Conversion
2+
// @author:jxdeng3989
3+
4+
class Solution {
5+
public:
6+
string convert(string s, int numRows) {
7+
string retstr;
8+
if(1==numRows)
9+
return s;
10+
for(int i=0; i<numRows; ++i)
11+
{
12+
retstr.push_back(s[i]);
13+
int maxspan = 2*(numRows-1);
14+
int span1 = maxspan-i*2;
15+
int span2 = maxspan - span1;
16+
int cntpos = i;
17+
if(span1==0)
18+
span1 = span2;
19+
if(span2==0)
20+
span2 = span1;
21+
while(1)
22+
{
23+
if(cntpos+span1>=s.size())
24+
break;
25+
cntpos += span1;
26+
retstr.push_back(s[cntpos]);
27+
28+
if(cntpos+span2>=s.size())
29+
break;
30+
cntpos += span2;
31+
retstr.push_back(s[cntpos]);
32+
}
33+
}
34+
return retstr;
35+
}
36+
};

0 commit comments

Comments
 (0)