Skip to content

Commit 692721f

Browse files
committed
add flatten.cpp
1 parent 7c48931 commit 692721f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/coundAndSay.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n^2)
2+
// Space Complexity: O(n)
3+
4+
class Solution {
5+
public:
6+
string countAndSay(int n) {
7+
string s{"1"};
8+
while(--n) {
9+
s = getNext(s);
10+
}
11+
return s;
12+
}
13+
14+
private:
15+
string getNext(const string &s) {
16+
stringstream ss;
17+
for(auto i = s.begin(); i != s.end();) {
18+
auto j = find_if(i, s.end(), bind1st(not_equal_to<char>(), *i));
19+
ss << distance(i, j) << *i;
20+
i = j;
21+
}
22+
return ss.str();
23+
}
24+
};

0 commit comments

Comments
 (0)