We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f4b8abd commit f90a731Copy full SHA for f90a731
solution/006.ZigZag Conversion/Solution.cpp
@@ -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
30
+ cntpos += span2;
31
32
+ }
33
34
+ return retstr;
35
36
+};
0 commit comments