Count and say,output limit exceed,why?


  • 0
    W
        class Solution {
    public:
        string countAndSay(int n); 
    };
    string Solution::countAndSay(int n){
        int i=0;
        int m=0;
        string s="1";
        while(m<n-1&&n>1)
        {
    		int i=0;
            int len=s.length();
            i=0;
             int j=0;
            stringstream ss;
    		int count=0;
            for(i=0;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;
    }

Log in to reply
 

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