forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0006.cpp
More file actions
32 lines (23 loc) · 702 Bytes
/
Copy path0006.cpp
File metadata and controls
32 lines (23 loc) · 702 Bytes
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
class Solution {
public:
string convert(string s, int numRows) {
int len = s.length();
if(numRows > len || numRows <= 1)
return s;
//use mov for sense of direction to push_back values
int row = 0;
int mov = -1;
vector<char> zigzag[numRows];
for(auto elem:s){
zigzag[row].push_back(elem);
if(row == 0 || row == numRows - 1)
mov *= -1;
row += mov;
}
string ans = "";
for(auto i:zigzag)
for(auto j:i)
ans += j;
return ans;
}
};