Why did i got MLE


  • 0
    V

    I got the MLE problem in my solution, can anybody help me?
    My solutution list:

    class Solution{
    public:
    	string convert(string s, int nRows){
    		int x = 2 * (nRows - 1);
    		int interval = x;
    		int len = s.size();
    		ans = "";
    		int first = 0, now = 0;
    		a = "";
    		while(first < nRows) {
    			a = "";
    			now = first;
    			while(now < len) {
    				//a.insert(a.size() - 1, 1, s[now]);
    				a += s[now];
    				if(x && x != interval) a += s[now + x];
    				now += interval;
    			}
    			first ++;
    			x -= 2;
    			ans += a;
    		}
    		return ans;
    	}
    private:
    	string a, ans;
    };

  • 2
    W

    Consider when nRows = 1, your loop while (now < len) will be a dead loop, because interval = x = 2 * (1 - 1) = 0.


Log in to reply
 

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