3707 {
3708 if(numRows == 1) {
3709 return s;
3710 }
3711 auto oss = ostringstream();
3712 auto *m = new char *[numRows];
3713 for(int i = 0; i < numRows; i++) {
3714 m[i] = new char[s.length()];
3715 memset(m[i], ' ', s.length() * sizeof(char));
3716 }
3717 int current_x = 0;
3718 int current_y = 0;
3719 bool dir = true;
3720 for(const char ch: s) {
3721 m[current_x][current_y] = ch;
3722 if(dir && current_x == numRows - 1 || !dir && current_x == 0) {
3723 dir = !dir;
3724 }
3725 if(dir) {
3726 current_x++;
3727 } else {
3728 current_x--;
3729 current_y++;
3730 }
3731 }
3732 for(int i = 0; i < numRows; i++) {
3733 for(int j = 0; j <= min(current_y, static_cast<int>(s.length() - 1)); j++) {
3734 if(m[i][j] != ' ') {
3735 oss << m[i][j];
3736 }
3737 }
3738 }
3739 for(int i = 0; i < numRows; i++) {
3740 delete[] m[i];
3741 }
3742 delete[] m;
3743 return oss.str();
3744 }