class Solution {

public:

string convert(string s, int numRows) {

if (s.empty() || numRows < 1)

return string("");

```
string result("");
int i, j;
for (i = 1; i <= numRows; ++i) {
int idx1 = i;
int idx2 = 2 * numRows - i;
for (; idx2 <= s.length(); ) {
if (idx1 < idx2) {
result += s[idx1 - 1];
idx1 += 2 * numRows - 2;
}
else if (idx1 > idx2) {
result += s[idx2 - 1];
idx2 += 2 * numRows - 2;
}
else {
result += s[idx1 - 1];
idx1 += 2 * numRows - 2;
idx2 += 2 * numRows - 2;
}
}
}
return result;
}
```

};