This commit is contained in:
2022-11-22 18:06:14 +08:00
parent 9a6595462d
commit 8ce5ca9166
3 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#include "s0038_count_and_say.hpp"
string Solution::countAndSay(int n) {
if (n == 1) {
return "1";
} else if (n <= 0) {
return "";
}
string prev = countAndSay(n - 1);
string result{""};
int len = prev.length();
int pos{0}, start{0};
while (pos < len) {
while (pos < len && prev[pos] == prev[start]) {
pos++;
}
result += to_string(pos - start) + prev[start];
start = pos;
}
return result;
}