Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 38. Count and Say
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
string countAndSay(int n) {
string curr = "1";
if (n == 1) return curr;
for (int i = 2; i <= n; i++) {
string next = "";
int cnt = 1;
char ele = curr[0];
for (int j = 1; j < curr.size(); j++) {
if (curr[j] == ele) {
cnt++;
} else {
next += to_string(cnt) + ele;
ele = curr[j];
cnt = 1;
}
}
next += to_string(cnt) + ele;
curr = next;
}
return curr;
}
};
Loading