forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_and_Say.cc
More file actions
28 lines (28 loc) · 775 Bytes
/
Copy pathCount_and_Say.cc
File metadata and controls
28 lines (28 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
string countAndSay(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string cur = "1";
if (n <= 1) {
return cur;
}
string nxt;
int count;
for (int i = 2; i <= n; i++) {
nxt = "";
count = 0;
for (int i = 0; i < cur.length(); i++) {
count = 1;
while (i + 1 < cur.length() && cur[i] == cur[i + 1]) {
count++;
i++;
}
nxt.append(1, (char)(count + '0'));
nxt.append(1, cur[i]);
}
cur = nxt;
}
return cur;
}
};