A very simple accepted C++ solution with only about 10 lines codes.(O(n))


  • 0
    1

    class Solution {
    public:
    string convert(string s, int numRows) {
    if(numRows == 1 || s == "") return s;
    string strs[numRows], ans = "";
    for(int i = 0, addr = 0, asc = 1; i < s.length(); ++i)
    {
    if(asc) strs[addr++] += s[i];
    else strs[addr--] += s[i];
    if(addr == numRows) addr -= 2, asc = 0;
    if(addr == -1) addr += 2, asc = 1;
    }
    for(int i = 0; i < numRows; ++i)
    ans += strs[i];
    return ans;
    }

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.