. Count and Say

Source: Internet
Author: User

The Count-and-say sequence is the sequence of integers with the first five terms as following:

1.     .     113.     214.     12115.     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 where 1≤ n ≤30, generate the n-th term of the count-and-say sequence.

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

Example 1:

Input:1output: "1"

Example 2:

Input:4output: "1211"

AC Code:

Class Solution {public:    string Countandsay (int n) {        vector<string> v;        Init (v, n);        return v[n-1];    }    void Init (vector<string>& v, int n) {        v.push_back ("1");        for (int i = 1; I <= n; ++i) {            string res = v[i-1];            char C = res[0];            String ans = "";            int num = 1;            for (int i = 1; i < res.length (); ++i) {                 if (res[i] = = c) {                    num++;                    Continue;                } else {                    ans = ans + to_string (num) + C;                    c = res[i];                    num = 1;                }             }            Ans = ans + to_string (num) + C;            V.push_back (ANS);}}    ;
Runtime:8 MS, faster than 8.70% of C + + online submissions for Count and Say.

Class Solution {public:std::string Countandsay (int n) {if (0 = = N) return "";        if (1 = = N) return "1";    std::string res= "1";        std::string s;                        for (int i = 1; i < n; i++) {//run from starting to generate second string int len = Res.size (); Cheack all digits in the string for (int j = 0; J < Len; J + +) {int count=1;//We Ha  ve at least 1 occourence for each digit//get the number of times same digit occurred (being carefull with    The end of the string) while ((j + 1 < len) && (res[j] = = res[j + 1])) {count++;        j + +; We need to keep increasing the index inside of the string}//Add to New string "count" +    "Digit itself" s + = std::to_string (count) + res[j];        }//Save temporary result res = s;        Clean our string-helper s.clear ();    } return res; }};
Runtime:4 MS, faster than 41.19% of C + + online submissions for Count and Say.

char* Countandsay (int n) {    if (n = = 1) return "1", char *cur = malloc (2), *tmp;cur[0] = ' 1 '; cur[1] = 0;int len, idx, J  , count;for (int i = 2; I <= n; ++i) {len = strlen (cur); tmp = malloc (len * 3); memset (tmp, 0, Len * 3); count = 1;for (idx = 1, j = 0; IDX < Len; ++IDX) {if (cur[idx] = = Cur[idx-1])        {            ++count;        } else        {            tmp[j++] = ' 0 ' + count;            Tmp[j++] = cur[idx-1];            Count = 1;}        } tmp[j++] = ' 0 ' + count;    Tmp[j++] = cur[len-1];free (cur); cur = tmp;} return cur;}

Runtime: 0 ms, faster than 100.00% of C online submissions for Count and Say.

. 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.