Very intuitive solution, just keep looping from top to bottom


  • 0
    M
    string convert(std::string s, int nRows) {
        if(nRows <= 1)
            return s;
        vector<std::string> temp(nRows);
        string res;
        int index = 0;
        int flag = 0;
        for(int i = 0; i< s.size(); i++){
            if(index == 0)
                flag = 1;
            if(index == nRows -1)
                flag = 0;
            temp[index].push_back(s[i]);
            if(flag == 1)
                index++;
            if(flag == 0)
                index--;
        }
        for(int i = 0; i< nRows;++i){
            res+=temp[i];
        }
        return res;
        
    }

Log in to reply
 

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