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.
2 parents 4d2833b + 837ebd9 commit d3210caCopy full SHA for d3210ca
06. Zigzag Conversion
@@ -0,0 +1,30 @@
1
+class Solution {
2
+public:
3
+ string convert(string s, int nums) {
4
+ int n = s.length();
5
+ if(nums <= 1 || nums> n)
6
+ return s;
7
+
8
+ vector<string> str(nums);
9
+ int pos = -1; // can be -1 or 1
10
+ int row = 0;
11
+ for(auto c: s)
12
+ {
13
+ string st;
14
+ str[row].push_back(c);
15
+ st+= c;
16
+ if(row == 0 || row == nums-1) // for checking our position in the zig zag whether top or bottom
17
18
+ pos*=-1; // for the zig zag pattern
19
+ }
20
+ row+=pos; // if position is decremented this means that we're accessing the elements in the diagonal
21
22
23
+ string temp = "";
24
+ for(auto c: str)
25
+ for(auto ch: c)
26
+ temp+=ch;
27
28
+ return temp;
29
30
+};
0 commit comments