标签:
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 as "one
2
, then one 1"
or 1211
.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
2读1,3读2,n读n-1,根据上面的规则。
class Solution { public: string work(string s) { int k=0; string t; while(k<s.size()) { int tmp=s[k]; int count=0; while(s[k]==tmp) { k++; count++; } t.push_back(count+'0'); t.push_back(tmp); } return t; } string countAndSay(int n) { if(n==1)return "1"; string tmp="1"; while(--n) { tmp=work(tmp); //迭代 } return tmp; } };
标签:
原文地址:http://blog.csdn.net/u011391629/article/details/52094195