-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.cpp
More file actions
60 lines (57 loc) · 1.22 KB
/
CountAndSay.cpp
File metadata and controls
60 lines (57 loc) · 1.22 KB
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
/***
* 模拟
* 循环n次,数出连续的数字下一个字符串先存个数再存数字
***/
/*
class Solution {
public:
string countAndSay(int n) {
if (n <= 0)
return "";
string spre = "1";
while (--n)
{
string scur;
int i = 0;
while (i < spre.size())
{
char c = spre[i];
int j = i + 1;
while ((j < spre.size()) && (spre[j] == c))
++j;
scur += to_string(j - i) + c;
i = j;
}
spre = scur;
}
return spre;
}
};
*/
/***
* 法2:同法1,另外一种itos的方法
***/
class Solution {
public:
string countAndSay(int n) {
if (n <= 0)
return "";
string spre = "1";
while (--n)
{
stringstream scur;
int i = 0;
while (i < spre.size())
{
char c = spre[i];
int j = i + 1;
while ((j < spre.size()) && (spre[j] == c))
++j;
scur << (j - i) << c;
i = j;
}
scur >> spre;
}
return spre;
}
};