-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem6.cpp
More file actions
41 lines (34 loc) · 999 Bytes
/
problem6.cpp
File metadata and controls
41 lines (34 loc) · 999 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
33
34
35
36
37
38
39
40
41
class Solution {
public:
string convert(string s, int numRows) {
if(s.length()<=numRows || numRows==1) {
return s;
}
//TC: O(n) worst time O(n^2)
vector<vector<char>> res(numRows, vector<char>(s.length(), ' '));
int i =0 ;
int ai = 0;
int aj = 0;
while (i < s.length()) {
while (ai <numRows && i < s.length()) {
res[ai][aj] = s[i];
i++; ai++;
}
ai-=2;
aj++;
while (ai > 0 && i < s.length()) {
res[ai][aj] = s[i];
ai--; aj++;
i++;
}
}
string ans = "";
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < s.length(); c++) {
if (res[r][c] != ' ')
ans += res[r][c];
}
}
return ans;
}
};