Skip to content

Commit 2564135

Browse files
committed
2
1 parent 085460b commit 2564135

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

CountandSay/CountandSay.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
string countAndSay(int n) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
7+
string begin = "1";
8+
for (int i = 0; i < n - 1; i++) {
9+
string next;
10+
int start = 0;
11+
while (start < begin.size()) {
12+
int end = start;
13+
int count = 0;
14+
while (end < begin.size() && begin[start] == begin[end]) {
15+
end += 1;
16+
count += 1;
17+
}
18+
next += char('0' + count);
19+
next += begin[start];
20+
start = end;
21+
}
22+
begin = next;
23+
}
24+
return begin;
25+
}
26+
};

0 commit comments

Comments
 (0)