-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.cpp
33 lines (31 loc) · 838 Bytes
/
6.cpp
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
#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
string convert(string s, int nRows) {
if(nRows < 2)
return s;
string r;
int n = s.size() / ( 2 * nRows - 2) + (s.size () % ( 2 * nRows - 2) == 0 ? 0 : 1);
for(int i = 0 ; i< nRows ; i++) {
for(int j = 0; j < n; ++j) {
if(i == 0){
r += s[(2*nRows - 2) * j];
} else {
int left = ( 2 * nRows - 2) * j + i ;
int right = ( 2 * nRows - 2) * (j + 1) - i;
if( left < s.size())
r += s[left];
if(right > left && right < s.size())
r += s[right];
}
}
}
return r;
}
int main(){
string s("PAYPALISHIRING");
cout<<convert(s, 3);
return 1;
}