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.
Bo Master didn't wake up ... The difficulty of adding oneself to the problem ... can start from N n,1n ... Not necessarily from 1,11,21 ... But the idea is the same.
PackageLeetcode2; Public classCountandsay { Public StaticString Countsay (intN) { //return count (n,n);String[] S=NewString[n]; s[0]= "" +N; String Sub=s[0]; //System.out.println (s[0]); for(intj1=1;j1<n;j1++) {StringBuilder Snext=NewStringBuilder (); intI=1; intCount=1; while(i<sub.length ()) { if(Sub.charat (i) ==sub.charat (i-1) ) {count++; I++; }Else{Snext=Snext.append (count); Snext=snext.append (Sub.charat (i-1)); I++; Count=1; }} Snext=Snext.append (count); Snext=snext.append (Sub.charat (i-1)); Sub=snext.tostring (); } returnSub; } Public Static voidMain (string[] args) {//TODO auto-generated Method StubSystem.out.print (Countsay (4)); }}
38.Count and Say