Compact c++, one pass O(n) 12ms


  • 0
    T
    class Solution {
    public:
        string convert(string s, int numRows) {
            if (numRows==1) return s;
            string r;
            int len = s.length();
            int int1=2*(numRows-1),int2=0; //2 intervals
            for(int offset=0;offset<numRows;offset++){
                int i=offset;
                while(true){
                    if (i>=len) break;
                    if (int1) { r+=s.at(i); i+=int1;}
                    if (i>=len) break;
                    if (int2) { r+=s.at(i); i+=int2;}
                }
                int1-=2;
                int2+=2;
            }
            return r;
        }
    };
    

Log in to reply
 

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