-
Notifications
You must be signed in to change notification settings - Fork 0
/
056_Count_And_Say.cpp
56 lines (50 loc) · 1.22 KB
/
056_Count_And_Say.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string countAndSay(int n){
if(n == 1) return "1";
if(n == 2) return "11";
string str = "11";
for(int i = 3; i <= n; i++){
str += '$';
int len = str.length(), cnt = 1;
string tmp = "";
for(int j = 1; j < len; j++){
if(str[j] != str[j - 1]){
tmp += cnt + '0';
tmp += str[j - 1];
cnt = 1;
}
else
cnt++;
}
str = tmp;
}
return str;
}
};
// Reccursive
class Solution {
public:
string prev, res;
string& countAndSay(int n) {
if(n == 1){
res = "1";
return res;
}
prev = countAndSay(n-1);
const int N = prev.length();
int count,j;
res = "";
for(int i=0;i<N;){
count = 0;
for(j=i;prev[i]==prev[j];j++){
count++;
}
res += to_string(count) + prev[i];
i=j;
}
return res;
}
};