What does the "nth sequence" in the requirement mean?


  • 1
    R

    The count-and-say sequence is the sequence of integers beginning as follows:
    1, 11, 21, 1211, 111221, ...

    1 is read off as "one 1" or 11.
    11 is read off as "two 1s" or 21.
    21 is read off as "one 2, then one 1" or 1211.
    Given an integer n, generate the nth sequence.

    Note: The sequence of integers will be represented as a string.

    class Solution {
    public:
        string countAndSay(int n) {
            string s;
            cas(s,n);
            return s;
        }
        
        void cas(string &s, int n) {
            int curr=n%10;
            int counter=1;
            stringstream ss;
            n/=10;
            
            /* Base case */
            if(n==0) {
                ss << counter << curr;
                s=s+ss.str();
                return; 
            }
        
            while(n%10==curr) {
                counter++;
                n/=10;
            }
            
            cas(s,n);
            
            ss << counter << curr;
            s=s+ss.str();
        }
    };
    

    Submission Result: Wrong Answer

    Input: 1

    Output: "11"

    Expected: "1"


Log in to reply
 

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