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