如图:输出与预期结果符合,但提示解答错误。

 

题目:

代码:

class Solution {
public:
    string convert(string s, int numRows) {
        int len = 0 , res = 0 , sor = 0, pack = 0;
        pack = numRows * 2 - 2;
        len = s.size();
        if(numRows == 1||len<=numRows)
        return s;
        res = len % pack;
        sor = len / pack;
        if(sor == 0)
        res = -1;

        string s_f;
        for(int i = 0; i < numRows; i++)
        {   
            if(i != 0 && i != numRows - 1)
            {
            for(int j = 0; j < sor * 2 + res_cacu(i,res,len,numRows); j++)
            {
                if( (j+2)%2 == 1 )
                s_f.push_back(s[i + pack/2*(j-1)+pack - 2*i]);
                else s_f.push_back(s[i + pack*j/2]);
            }

            }
            else
            for(int j = 0 ; j < sor + res_cacu(i,res,len,numRows);j++)
            s_f.push_back(s[i + pack*j]);

        }
        
    return s_f;
       
    }
    private:
        int res_cacu(int &numRow,int &res,int &len,int &numRows)
        {
            if(res > numRow)
            return 1;
            else if(res == -1)
            {
                if(len >= numRows + (numRows - numRow - 1))
                return 2;
                else
                return 1;
            }else return 0;
        }

};

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐