feat: 38_count_and_say

This commit is contained in:
SquidSpirit 2025-02-27 00:33:36 +08:00
parent a66d6116b3
commit 828f3e0e7b

31
38_count_and_say/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string rle(string s) {
s.push_back('.');
string result;
int sameCount = 1;
for (int i = 1; i < s.length(); i++) {
if (s[i] == s[i - 1]) {
sameCount++;
} else {
result.append(to_string(sameCount) + to_string(s[i - 1] - '0'));
sameCount = 1;
}
}
return result;
}
string countAndSay(int n) {
string result = "1";
for (int i = 1; i < n; i++) {
result = rle(result);
}
return result;
}
};