diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_and_say.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'count_and_say.cpp')
-rw-r--r-- | count_and_say.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/count_and_say.cpp b/count_and_say.cpp new file mode 100644 index 0000000..a1086b4 --- /dev/null +++ b/count_and_say.cpp @@ -0,0 +1,26 @@ +#include "leetcode.h" + +class Solution { +public: + string countAndSay(int n) { + if (n == 0) + return ""; + string ans = "1"; + while (--n) { + string curr = ""; + for (int i = 0; i < ans.size(); i++) { + int count = 1; + while ((i + 1 < ans.size()) && (ans[i] == ans[i + 1])) + count++, i++; + curr += to_string(count) + ans[i]; + } + ans = curr; + } + return ans; + } +}; + +int main() { + Solution obj; + cout << obj.countAndSay(4); +} |