-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0038_countAndSay.cc
More file actions
59 lines (53 loc) · 908 Bytes
/
Problem_0038_countAndSay.cc
File metadata and controls
59 lines (53 loc) · 908 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
string process(int n)
{
if(n < 0)
{
return "";
}
if (n == 1)
{
return "1";
}
string pre = process(n - 1);
string cur = "";
char c = pre[0];
int count = 1;
for (int i = 1; i < pre.length(); i++)
{
if (pre[i] != c)
{
cur += std::to_string(count) + c;
c = pre[i];
count = 1;
}
else
{
count++;
}
}
cur += std::to_string(count) + c;
return cur;
}
string countAndSay(int n) { return process(n); }
};
void testCountAndSay()
{
Solution s;
EXPECT_TRUE("1" == s.countAndSay(1));
EXPECT_TRUE("1211" == s.countAndSay(4));
EXPECT_TRUE("111221" == s.countAndSay(5));
EXPECT_SUMMARY;
}
int main()
{
testCountAndSay();
return 0;
}