Count and Say LeetCode solution in C++

1
class Solution {
2
public:
3
string countAndSay(int n) {
4
if (n == 1)
5
return "1";
6
else {
7
string ans = "";
8
string temp = countAndSay(n - 1);
9
int count = 1, curr = temp[0], i = 1;
10
while (i < temp.length()) {
11
if (temp[i] == curr)
12
count++;
13
else {
14
ans += to_string(count);
15
ans += curr;
16
count = 1;
17
curr = temp[i];
18
}
19
i++;
20
}
21
ans += to_string(count);
22
ans += curr;
23

24
return ans;
25
}
26
}
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0