Skip to content

Commit 8a7e1ff

Browse files
authored
Create 38. Count and Say (#771)
2 parents d1e8065 + 1cf4f1c commit 8a7e1ff

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

38. Count and Say

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string countAndSay(int n) {
4+
string curr = "1";
5+
if (n == 1) return curr;
6+
for (int i = 2; i <= n; i++) {
7+
string next = "";
8+
int cnt = 1;
9+
char ele = curr[0];
10+
for (int j = 1; j < curr.size(); j++) {
11+
if (curr[j] == ele) {
12+
cnt++;
13+
} else {
14+
next += to_string(cnt) + ele;
15+
ele = curr[j];
16+
cnt = 1;
17+
}
18+
}
19+
next += to_string(cnt) + ele;
20+
curr = next;
21+
}
22+
return curr;
23+
}
24+
};

0 commit comments

Comments
 (0)