Memory Limit Exceeded in C++. Why?


  • 0
    L
    class Solution {
    public:
        string convert(string s, int nRows) {
            int i=0;
            size_t size = s.size();
            int j,step;
            string *str=new string();
            while(i<nRows)
            {
                j=i;
                step=2*(nRows-1-i);
                while(j<size)
                {
                    str->push_back(s[j]);
                    if(i==0 || i==nRows-1) j+=2*(nRows-1);
                    else {j+=step;step=2*(nRows-1)-step;}
                }
                ++i;
            }
            return *str;
        }
    };

  • 0
    B

    the same question


  • 0
    B

    check the case nRows = 1 , the inner while loop will be infinite.


Log in to reply
 

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