38.Count and Say
111211211111221
示例 1:
输入: 1
输出: "1"
示例 2:
输入: 4
输出: "1211"Last updated
111211211111221示例 1:
输入: 1
输出: "1"
示例 2:
输入: 4
输出: "1211"Last updated
class Solution {
private:
vector<string> say={"1","11","21","1211"};
public:
string countAndSay(int n) {
if(n<=say.size()) return say[n-1];
string tmp=countAndSay(n-1);
string res;
int k=1;
for(int i=1;i<tmp.length();i++)
{
if(tmp[i]==tmp[i-1])
k++;
else
{
res+=to_string(k)+tmp[i-1];
k=1;
}
}
res+=to_string(k)+tmp[tmp.length()-1];
say.push_back(res);
return res;
}
};