-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountAndSay.cpp.save
More file actions
49 lines (48 loc) · 1.05 KB
/
Copy pathCountAndSay.cpp.save
File metadata and controls
49 lines (48 loc) · 1.05 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
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
class Solution
{
public:
string countAndSay(int n)
{
if(n==0)
return NULL;
if(n==1)
return string("11");
string s="11";
int i;
size_t j,k=0;
for(i=2; i<=n; i++)
{
string tmp;
stringstream ss;
for(j=1; j<s.length(); j++)
{
if(s[k]!=s[j])
{
ss<<j-k;
tmp+=ss.str();
//注意清空stringstream,不然上次的内容还存在,还有清空操作不是clear()
ss.str("");
tmp.append(1,s[k]);
k=j;
}
}
ss<<j-k;
tmp+=ss.str();
ss.str("");
tmp.append(1,s[k]);
s=tmp;
k=0;
}
return s;
}
};
int main()
{
Solution s;
cout<<s.countAndSay(5)<<endl;
}