-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.cc
More file actions
40 lines (36 loc) · 898 Bytes
/
CountAndSay.cc
File metadata and controls
40 lines (36 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* https://oj.leetcode.com/problems/count-and-say/
*/
namespace CountAndSay {
class Solution {
public:
string countAndSay(int n) {
if (n == 0) {
return "";
}
if (n == 1) {
return "1";
}
string s = countAndSay(n - 1);
char curChar = s[0];
int count = 1;
int i = 1;
string res = "";
while (i < s.size()) {
if (s[i] == curChar) {
count++;
} else {
res += to_string(count);
res += curChar;
// reset
curChar = s[i];
count = 1;
}
i++;
}
res += to_string(count);
res += curChar;
return res;
}
};
}