Skip to content

Commit 4ef0c40

Browse files
Create count_and_say.cpp
1 parent a446174 commit 4ef0c40

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

count_and_say.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
cur += to_string(count);
23+
cur += prev.back();
24+
25+
return cur;
26+
}
27+
};

0 commit comments

Comments
 (0)