C++ code, the problem is solved very easy


  • 17
    A

    class Solution {
    public:
    string convert(string s, int nRows) {

        if(nRows==1)
            return s;
        
        int y=0;
        bool flag= true;
        string sArray[nRows];
        for(int i=0;i<nRows;i++)
            sArray[i]="";
        
        for(int i=0;i<s.length();i++){
            
            sArray[y]+=s[i];
            
            if(y==0){
                flag=true;
            }
            if(y==nRows-1){
                flag=false;
            }
            
            if(flag==true){
                y++;
            }else{
                y--;
            }
            
    
        }
    
        string ret="";
        
        for(int i=0;i<nRows;i++){
            ret+=sArray[i];
        }
        
        return ret;
    }
    

    };


  • 0
    Y
    This post is deleted!

  • 2
    T

    Same idea as your solution

    class Solution {
    public:
    string convert(string s, int nRows) {
        if(s.empty()||nRows<2)
            return s;
        vector<string> zig(nRows);
        bool down = true;
        for(int i = 0; i < s.size(); ++i)
        {
            int row = i%(nRows - 1);
            if(down)
                zig[row].push_back(s[i]);
    
            else
                zig[nRows - 1 - row].push_back(s[i]);
            if(row==nRows - 2)
                down = !down;
        }
        string res;
        for(auto& temp: zig)
            res += temp;
        return res;
    }
    };
    

    Basically, if nRows = 4, then 0 1 2 would go down from zig row 0 to 2, but 3 4 5 would go up from zig row 3 to 1. Then repeat this process.


  • 0
    B

    Similar solution,

    class Solution {
    public:
        string convert(string s, int nRows) {
            if (nRows <= 1)
                return s;
    
            vector<string> zigzag(nRows, "");
            int len = s.size();
            int state = 0, row = 0;
            for (int i = 0; i < len;) {
                if (state == 0) {
                    // go down
                    zigzag[row++] += s[i++];
                    if (row == nRows) {
                        row = nRows - 2;
                        state = 1; // go up
                    }
                } else {
                    // state == 1
                    // go up
                    // row from 1 to nRows - 2
                    if (row >= 1) {
                        zigzag[row--] += s[i++];
                        if (row == 0)
                            state = 0;
                    } else {
                        row = 0;
                        state = 0;
                    }
                }
            }
            string res;
            for (vector<string>::iterator it = zigzag.begin(); it != zigzag.end(); it++) {
                res += *it;
            }
            return res;
        }
    };

Log in to reply
 

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