Skip to content

Commit c199fae

Browse files
authored
Create 38. Count and Say1 (#772)
2 parents 8a7e1ff + e7e1ad4 commit c199fae

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Diff for: 38. Count and Say1

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
string ans="";
4+
bool found=false;
5+
void fn(string &temp,int n){
6+
if(found)return;
7+
if(n==1){
8+
ans=temp;
9+
found=true;
10+
return;
11+
}
12+
13+
char pre=temp[0];
14+
string s=temp;
15+
string str="";
16+
int cnt=1;
17+
for(int i=1;i<temp.length();i++){
18+
if(temp[i]==pre){
19+
cnt++;
20+
}else{
21+
str+=to_string(cnt);
22+
str+=pre;
23+
cnt=1;
24+
}
25+
pre=temp[i];
26+
}
27+
str+=to_string(cnt);
28+
str+=pre;
29+
fn(str,n-1);
30+
}
31+
string countAndSay(int n) {
32+
string temp="1";
33+
fn(temp,n);
34+
return ans;
35+
}
36+
};

0 commit comments

Comments
 (0)