Topic:
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 "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.
Code:
classSolution { Public: stringCountandsay (intN) {stringTMP1 ="1"; stringTMP2 =""; for(size_t i =1; I < n; ++i) {intDigit_count =1; for(size_t j =1; J < Tmp1.size (); ++j) {if(tmp1[j]==tmp1[j-1] ) { ++Digit_count; } Else{TMP2+ = digit_count+'0'; TMP2+ = tmp1[j-1]; Digit_count=1; }} TMP2+ = digit_count+'0'; TMP2+ = Tmp1[tmp1.size ()-1]; TMP1=TMP2; TMP2=""; } returnTMP1; }};
Tips
This test instructions is not very well understood.
To put it simply: the nth set of strings is the reading of the string n-1, and the criterion for reading is ' sequential number + number '.
The rest is dealing with the boundary case.
"Count and Say" CPP