Concise C++ code for ZigZag


  • 0
    D
    public:
        string convert(string s, int numRows) {
            string res = ""; int len = s.size();
            if(numRows >= len || numRows == 1) return s;
            
            for(int i = 0; i <numRows; ++i)
                for(int j = i, cnt = 0; j < len; ++cnt){
                    res += s[j];
                    if(j % (numRows - 1) == 0 ) j += (numRows - 1) * 2;
                    else if(cnt % 2 == 0) j += (numRows - 1 - i % (numRows - 1)) * 2;
                        else j += i % (numRows - 1) * 2;
                }
            return res;
        }
    };```

Log in to reply
 

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