class Solution {
public:
string countAndSay(int n) {
vector<string> results(n);
results[0] = "1";
for(int i = 1; i < n; ++i) {
results[i] = generate(results[i - 1]);
}
return results[n - 1];
}
string generate(string s) {
string result;
int i = 0;
while(i < s.length()) {
char c = s[i];
int j = i + 1;
while(j < s.length() && s[j] == c){
j++;
};
result += to_string(j - i) + c;
i = j;
}
return result;
}
};
No comments:
Post a Comment