-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount and Say
71 lines (67 loc) · 1.64 KB
/
Count and Say
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Solution {
public:
string countAndSay(int numm)
{
string a = "";
if (numm == 1)
{
return "1";
}
string prev = countAndSay(numm - 1);
int cnt = 1;
for (int i = 0; i < prev.length()-1; i++)
{
if (prev[i] == prev[i+1])
{
cnt++;
}
else
{
a+=to_string(cnt);
a+=prev[i];
cnt = 1;
}
}
a+=to_string(cnt);
a+=prev[prev.length()-1];
return a;
}
// string countAndSay(int numm) {
// string a="";
// // if(numm==0){
// // // a= a+ char(1+'0');
// // return a;
// // }
// if(numm==1){
// a= char(1 +'0')+a;
// return a;
// }
// string prev=countAndSay(numm-1);
// int n=prev.length()-1;
// int count=0;
// while(n>=0){
// char num = prev[n];
// count=0;
// while(n>=0 && prev[n]==num ){
// count++;
// // prev.pop_back();
// n--;
// }
// string c = num+to_string(count) ;
// a = a+c ;
// }
// reverse(a.begin(),a.end());
// return a;
// // int prevCount = stoi(prev);
// // while(prevCount){
// // int num=prevCount%10;
// // int temp=prevCount/10;
// // int count=1;
// // while(temp%10==num){
// // count++;
// // prevCount=temp;
// // }
// // a=char(count + '0') + char(num + '0') + a;
// // }
// }
};