原题链接:https://oj.leetcode.com/problems/count-and-say/
这道题其实考的还是细心了,外层循环n,内存循环当前字符长度。
class Solution {
public:
string countAndSay(int n) {
string res = "";
if (n < 0) return res;
res = "1";
int i = 1;
while (i < n) {
string temp = "";
char prev = '-';
int count = 0;
for (int j = 0; j < res.size(); ++j) {
if (prev != res[j]) {
if (prev != '-') {
temp.push_back('0' + count);
temp.push_back(prev);
}
prev = res[j];
count = 1;
} else {
++count;
}
}
temp.push_back('0' + count);
temp.push_back(prev);
++i;
res = temp;
}
return res;
}
};