-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountandSay.hpp
74 lines (60 loc) · 1.18 KB
/
CountandSay.hpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Count and Say
//
//The count - and - say sequence is the sequence of integers beginning as follows :
//1, 11, 21, 1211, 111221, ...
//
//1 is read off as "one 1" or 11.
//11 is read off as "two 1s" or 21.
//21 is read off as "one 2, then one 1" or 1211.
//Given an integer n, generate the nth sequence.
//
//Note: The sequence of integers will be represented as a string.
#include <string>
#include <iostream>
using namespace std;
class Solution
{
public:
std::string countAndSay(int n)
{
std::string res;
if (n <= 0)
return res;
std::string tmp = "1";
res = tmp;
--n;
while (n--)
{
tmp = countAndSay(tmp);
res = tmp;
}
return res;
}
std::string countAndSay(std::string& strNum)
{
std::string res;
std::size_t cnt = 1;
for (size_t i = 1; i < strNum.size(); i++)
{
if (strNum[i] != strNum[i - 1])
{
res += myitoa(cnt);
res.push_back(strNum[i - 1]);
cnt = 1;
}
else
++cnt;
}
res += myitoa(cnt);
res.push_back(strNum.back());
return res;
}
std::string myitoa(int n)
{
char buffer[11];
memset(buffer, sizeof(buffer), 0);
//_itoa(n, buffer, 10);
sprintf(buffer, "%d", n);
return std::string(buffer);
}
};