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