-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-and-say(AC).cpp
More file actions
42 lines (36 loc) · 1006 Bytes
/
Copy pathcount-and-say(AC).cpp
File metadata and controls
42 lines (36 loc) · 1006 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
36
37
38
39
40
41
42
// 1CE, 1AC
#include <cstdio>
using namespace std;
class Solution {
public:
string countAndSay(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
string res = "1";
for(int i = 1; i < n; ++i){
res = nextSequence(res);
}
return res;
}
private:
char buf[100];
string nextSequence(string cur) {
string res;
int i, j;
int len = cur.length();
res = "";
i = 0;
while(i < len){
j = i + 1;
while(j < len && cur[i] == cur[j]){
++j;
}
// 1CE here, you can't simply concatenate string with other data type...
// use sprintf or sstream to do this
sprintf(buf, "%d%c", j - i, cur[i]);
res = res + string(buf);
i = j;
}
return res;
}
};