Leetcode:count and Say

Source: Internet
Author: User

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

1is read off as "one 1" or 11 .
11is read off as "two 1s" or 21 .
21is read off "one 2 as, then one 1" or 1211 .

Given an integer n, generate the nth sequence.

Note:the sequence of integers would be represented as a string.

The title means n=1 when the output string 1;n=2, the number of numbers in the last string, because the last string has a 1, so the output 11;n=3, because the last character is 11, there are 2 1, so the output 21;n=4, because the last string is 21, there is a 2 and a 1, So the output is 1211. And so on (constant number), write a Countandsay (n) function to return the string.

Class Solution {public:    string convert (const string &say)    {       StringStream ss;    Create a stream       int count=0;       Char last=say[0];       for (size_t i=0; i<=say.size (); ++i)       {           if (Last==say[i])  //each time compared to the previous character is equal, equals count plus one           {               + + Count;           }           else           {           ss<<count<<last;   The value of count is passed to the stream SS, and the last value is passed into the           count=1;           Last=say[i];           }       }       return Ss.str ();   Returns the string in the stream    }        string Countandsay (int n) {        if (n<=0) return string ();         String say= "1";    Defines the contents of the initial string for        (int i=1; i<n; ++i)        {            Say=convert (say);        }        return say;}    ;

Or:

Class Solution {Public:string Nextread (string s) {    stringstream ss;    int count, i = 0, n = s.length ();    while (I < n) {        count = 0;        while (i + 1 < n && s[i] = = s[i + 1]) {            i++;            count++;        }        SS << Count + 1 << s[i];        i++;    }    return Ss.str ();} string Countandsay (int n) {    string res = "";    if (n = = 0) return res;    res = "1";    if (n = = 1) return "1";    while (n > 1) {        res = nextread (res);        n--;    }    return res;}};

Other solutions:

Class Solution {public:   string Countandsay (int n) {    if (n==0) {        return NULL;    }    if (n==1) {        return "1";    }    int count=1;    String s1= "1";    string S2;    int j=1;    if (n>=2) {        s1= "one";        j=2;    }    int i=0;    while (J<n && n>1) {        s2= "";        For (I=0;i<s1.size (), i++) {for            (int m=i;m<s1.size () -1;m++) {                if (s1[m]==s1[m+1]) {                  count++;                  i++;                }                if (S1[m]!=s1[m+1]) {break                ;                 }            }                S2+=to_string (count) +s1[i];                count=1;        }        S1=S2;        j + +;    }    return s1;};

  

Class Solution {public:string Countandsay (int n) {    if (n==0)  return "";    if (n==1)  return "1";    String str= "1";    string tmp;    for (int i=1;i<n;++i)    {        tmp.clear ();        int cnt=1;        for (int j=0;j<str.size (), ++j)        {while            (J+1<str.size ())            {                if (str[j]==str[j+1])                {                    ++cnt;                    ++j;                }                else break                ;            }            Tmp.push_back (cnt+ ' 0 ');            Tmp.push_back (Str[j]);            cnt=1;        }        str=tmp;    }    return str;};

  

Leetcode:count and Say

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.