https://leetcode.com/problems/count-and-say/
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.
Ideas:
Test instructions somewhat blurred, see 1211 to 111221 understand.
AC Code:
1 classSolution {2 Public:3 stringCountandsay (intN) {4 inttmp_n,count,k;5 stringTmp,tmp_res,a[n];6a[0]="1";7 for(intI=1; i<n;i++){8tmp=a[i-1];9tmp_res="";Tentmp_n=tmp.size (); Onek=0; ACount=1; - for(intj=1; j<tmp_n;j++){ - if(tmp[j]==tmp[j-1]){ thecount++; - Continue; - } - Else{ +Tmp_res.push_back ('0'+count); -Tmp_res.push_back (tmp[j-1]); +Count=1; A } at } -Tmp_res.push_back ('0'+count); -Tmp_res.push_back (tmp[tmp_n-1]); -a[i]=Tmp_res; - } - returna[n-1]; in } -};
Leetcode (38): Count and Say