Leetcode- Count and Say
Q:
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.
1 classSolution {2 Public:3 stringCountandsay (intN) {4 intCount=1;5 stringseq, result;6 if(n==1){7 return "1";8 }9 //if (n==2) {Ten //return "one"; One // } ASeq = Countandsay (n1); - string:: Iterator iter_seq = seq.begin () +1; - while(iter_seq!=Seq.end ()) { the if(*iter_seq = = * (iter_seq-1)){ -count++; -iter_seq++; - } + Else{ -Result.push_back ('0'+count); +Result.push_back (* (iter_seq-1)); ACount=1; atiter_seq++; - } - } -Result.push_back ('0'+count); -Result.push_back (* (iter_seq-1)); - returnresult; in } -};
My train of thought: recursion.
relatively simple. Note how to convert a number to char, Push_back only accepts Char
http://blog.csdn.net/talentluke/article/details/6050617
Http://zhidao.baidu.com/link?url=i4aEJh3bn_QWQC-DyXjF3eEorCMklZiEWGg2u8N8MdPl4-HkKQIvL8FazbX7UeRhK3WetF-XXCctpm5ElYHXaa
Leetcode-count and Say