您的位置:首页 > 其它

[Leetcode] 38 - Count and Say

2015-01-29 17:18 357 查看
原题链接: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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: