Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 664 Bytes

1-30-convert.md

File metadata and controls

29 lines (24 loc) · 664 Bytes

题目

思路

  • 一个变量控制向上还是向下的方向,用一个字符串数组进行记录。

实现

string convert(string s, int numRows) {
      if(numRows==1) return s;
      string res;
      int len = min(int(s.size()), numRows);
      vector<string> rows(len);
      bool goDown = false;
      int currRow = 0;
      for(char c:s){
        rows[currRow] += c;
        if(currRow == 0 || currRow == len - 1) goDown = !goDown;
        currRow += goDown? 1 : -1;
      }
      for(string row:rows){
        res += row;
      }
      return res;
    }