We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a446174 commit 4ef0c40Copy full SHA for 4ef0c40
count_and_say.cpp
@@ -0,0 +1,27 @@
1
+class Solution {
2
+public:
3
+ string countAndSay(int n) {
4
+ if (n == 1)
5
+ return "1";
6
+
7
+ string prev = countAndSay(n-1);
8
+ string cur = "";
9
+ int count = 1;
10
11
+ for (int i = 0; i < prev.length()-1; i++) {
12
+ if (prev[i] == prev[i+1]) {
13
+ count++;
14
+ }
15
+ else {
16
+ cur += to_string(count);
17
+ cur += prev[i];
18
+ count = 1;
19
20
21
22
23
+ cur += prev.back();
24
25
+ return cur;
26
27
+};
0 commit comments