Leetcode 38 Count and Say 传说中的递推
1 class Solution { 2 public: 3 vector<string> vs_; 4 Solution(){ 5 string t("1"); 6 vs_.push_back(t); 7 for(int i = 1; i< 30;++i){ 8 string t1 = vs_[i - 1]; 9 t = ""; 10 int cnt = 1,j ; 11 12 for(j = 0; j < t1.size() - 1; ++j){ 13 if(t1[j] == t1[j + 1]){ 14 ++cnt; 15 } 16 else{ 17 char s[20] =""; 18 sprintf(s,"%d%c",cnt,t1[j]); 19 t += string(s); 20 cnt = 1; 21 } 22 } 23 char s[20] =""; 24 sprintf(s,"%d%c",cnt,t1[j]); 25 t += string(s); 26 vs_.push_back(t); 27 } 28 } 29 ~Solution(){ 30 vs_.clear(); 31 } 32 33 string countAndSay(int n) { 34 return vs_[n-1]; 35 } 36 };