Leetcode 38. 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 meaning of the subject to understand, I just started not to understand what the meaning of

1.1

2.11

3.21

4.1211

5.111221

Count and say this is how to enter 1 Output 1, the second line is regular, the first line is a 1, the second row of the string is a 1 connected to 11

Similarly the third line is 2 1, the fourth line is a 2, a 1 ....

Understand the meaning of the topic, it will not be so difficult to do

classSolution { Public:    stringCountandsay (intN) {stringPre ="1", current;  for(inti =2; I <= n;++i) { current=""; CharPrechar = pre[0]; intCount =0;  for(intj =1; J < Pre.size (); + +j) {++count; if(Pre[j]! =Prechar) {Current.insert (current),Char(Count + -));                                        Current.insert (End (current), Prechar); Prechar=Pre[j]; Count=0; }} current.insert (current),Char(Count +1+ -));            Current.insert (End (current), Prechar); Pre=Current ; }        returnPre; }};

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