-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#6.cc
More file actions
27 lines (27 loc) · 740 Bytes
/
LeetCode#6.cc
File metadata and controls
27 lines (27 loc) · 740 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
string ret ;
class Solution {
public:
string convert(string s, int nRows) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(nRows==1) return s; //Important
ret = "";
int len = s.length();
for(int i=1;i<=nRows;i++){
int ind = i;
while(ind<=len){
ret += s[ind-1];
if(i==1 || i==nRows){
ind += nRows+nRows-2;
}
else{
int t = ind + nRows*2-i*2;
if(t <= len)
ret += s[t-1];
ind += nRows+nRows-2;
}
}
}
return ret;
}
};