Why 1 Expected: "1"? isn't it "11"?


  • 2
    D

    why 1 Expected: "1"? isn't it "11"?


  • 0
    D

    The n is for level number instead of direct passed digit. So need to use recursive to fetch the n-1' s result for n's result . My solution.

    class Solution {
    public:
        string countAndSay(int n) {
            string ret="";
            string str="";
            stringstream cCnt;
            string cCnt_str;
            if(n==1)return "1";
            int cnt=0;
            str=countAndSay(n-1);
            
            char chr=str[0];
            for(int i=0;i<str.size();i++)
                if(str[i]==str[i+1])
                    {
                    cnt++;
                    chr=str[i];
                    }
                else
                    {
                    cCnt<<(++cnt);
                    cCnt>>cCnt_str;
                    ret+=cCnt_str+chr;
                    cnt=0;
                    chr=str[i+1];
                    cCnt.clear();
                    }
                    
            return ret;
        }
    };

  • 0
    D

    stringstream cCnt has to been clear() after each use or it somehow causes unexpected output, where I spend much time on tuning it.


  • 1

    why 1 Expected: "1"? isn't it "11"?

    Because the first sequence is "1", not "11".
    "11" is the second sequence.


  • 0
    C

    Thanks for trying to clarify but I'm confused as well even with this direction. The description states the following.
    "1 is read off as "one 1" or 11." Is 1 not the input and 11 the output?


  • 0
    D

    Thanks for your answer. I got the meaning of 'n' finally.


  • 0

    @codelogic As the problem says, the sequence is 1, 11, 21, 1211, 111221, ...

    So:

    Return 1 when asked for the first element (n=1).
    Return 11 when asked for the second element (n=2).
    Return 21 when asked for the third element (n=3).
    And so on.


  • 0
    C

    "@codelogic As the problem says, the sequence is 1, 11, 21, 1211, 111221, ..."
    Yes but that sentence could mean output sequence or input sequence its not specified in the problems description and that's where my confusion came about. Thank you for the clarification I appreciate it.


  • 0
    C

    Oh wow sorry when I came back to this problem I got confused in yet another way. So my last reply reflected the new confusion lol.


Log in to reply
 

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