Share my 26 ms solution


  • 1
    W
    class Solution {
    public:
        string convert(string s, int nRows) {
            string result;
            string::iterator it;
            bool state; 
            it=s.begin();
            if(nRows==1)
                return s;
            for(int i=0;i<nRows;++i)
            {
                it=s.begin()+i;
                state=true;
                if(i==(nRows-1)||i==0)
                {
                    while(it<s.end())
                    {
                        result.push_back(*it);
                        it=it+2*(nRows-1);
                    }
                }
                else
                {
                    while(it<s.end())
                    {
                        result.push_back(*it);
                        if(state)
                            it=it+2*(nRows-i-1);
                        else
                            it=it+2*i;
                        state=!state;
                    }
                }
            }
            return result;
        }
    };

Log in to reply
 

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