-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount and Say.h
More file actions
35 lines (33 loc) · 793 Bytes
/
Count and Say.h
File metadata and controls
35 lines (33 loc) · 793 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
class Solution {
public:
string convert(string digits){
string ret = "";
int i = 0;
char digit = digits[0];
int count = 0;
for(int i = 0; i < digits.size(); ++i) {
if(digits[i] == digit){
count++;
}else{
stringstream ss1;
ss1 << count;
ret += ss1.str() + digit;
count = 1;
digit = digits[i];
}
}
stringstream ss1;
stringstream ss2;
ss1 << count;
ret += ss1.str() + digit;
return ret;
}
string countAndSay(int n) {
string num = "1";
while(n-1){
num = convert(num);
n--;
}
return num;
}
};