Skip to content

Latest commit

 

History

History
22 lines (21 loc) · 516 Bytes

6. ZigZag Conversion.md

File metadata and controls

22 lines (21 loc) · 516 Bytes
class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) return s;
        vector<string> arr(numRows);
        int row = 0, step = 1;
        for (int i = 0; i < s.length(); ++i) {
            arr[row].push_back(s[i]);
            if (row == 0) step = 1;
            else if (row == numRows - 1) step = -1;
            row += step;
        }
        string ret;
        for (const string& str : arr) {
            ret += str;
        }
        return ret;
    }
};