6. ZigZag Conversion

来源:互联网 发布:天猫和淘宝的盈利模式 编辑:程序博客网 时间:2024/06/09 23:49

水题。




class Solution {                                    public:                                             string convert(string s, int numRows) {                                                             if (1 == numRows) {                                 return s;                                   }                                                                                               vector<string> v_s(numRows);                                                                    int length = s.length();                        int next_line = 0;                              bool direction = true;                          for (int idx = 0; idx < length; idx++) {                v_s[next_line] = v_s[next_line] + s[idx];                                                                if (direction) {                                    next_line++;                                } else {                                            next_line--;                                }                                                                                               if (next_line >= numRows) {                            direction = !direction;                            next_line -= 2;                             }                                                                                               if (next_line < 0) {                                direction = !direction;                            next_line += 2;                             }                                           }                                                                                               string result;                                  for (int idx = 0; idx < numRows; idx++) {                result += v_s[idx];                         }                                                                                               return result;                              }                                       };  


原创粉丝点击