-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.cpp
More file actions
30 lines (30 loc) · 700 Bytes
/
Copy path6.cpp
File metadata and controls
30 lines (30 loc) · 700 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
class Solution {
public:
string convert(string s, int numRows) {
if(numRows == 1) return s;
vector<string> local(numRows, "");
int i=0;
bool zig = true;
int idx = 0;
for(int i=0; i<s.length(); i++){
local[idx] = local[idx]+s[i];
if(zig) idx++;
else idx--;
if(zig && idx > numRows-1){
zig = false;
idx = numRows-2;
if(idx == 0){
zig = true;
idx = 0;
}
}
else if(!zig && idx <= 0){
zig = true;
idx = 0;
}
}
string result = "";
for(string str : local) result += str;
return result;
}
};