-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountandSay.cpp
33 lines (33 loc) · 898 Bytes
/
CountandSay.cpp
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
// start is the begining, count records the number, so
//once the next one is different from the privious,
//count should turn back to 1.
class Solution {
public:
string countAndSay(int n) {
string s;
if(n <= 0) return s;
s = "1";
int start = 1; //start from 1
while(start < n)
{
string tmp;
int count = 1; // the consecutive same number
for(int j = 1; j < s.length(); ++j)
{
if(s[j] == s[j-1])
++count;
else
{
tmp.push_back(count + '0');
tmp.push_back(s[j-1]);
count = 1;
}
}
tmp.push_back(count + '0');
tmp.push_back(s[s.length()-1]);
s = tmp;
++start;
}
return s;
}
};