Count and say,output limit exceeded,can you tell me why?thank you!!


  • 0
    W
    class Solution {
    public:
        string countAndSay(int n)
        {
        int i=0;
        int m=0;
        if(n<=0)
            return '\0';
        string s("1");
        stringstream ss;
        while(m<n-1&&n>1)
        {
    		int i=0;
            int len=s.length();
            int j=0;
    
    		int count=0;
            for(;i<len;i++)
            {
    			if(s[i]==s[j])
    				count++;
    			else
    			{
    				ss<<count;
    				ss<<s[j];
    				j=i;
    			}
            }
    		ss<<count;
    		ss<<s[j];
            s=ss.str();
            m++;
        }
        return s;
        }
       
    };

  • 0
    M

    Had the same one. It seems the checked first runs all tests and only then compares to expected results. Thus if for some big n the result is not only wrong but also too big it crashes before making the actual comparison.

    IMHO a bit unfair, running anc comparing one-by-one would let one figure out the actual problem.

    Try adding something like:
    if (n <= 10)
    return string("");

    To see the actual failure.
    Mine was at n=6 :)


Log in to reply
 

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