您的位置:首页 > 其它

[LeetCode] Count and Say

2015-08-17 17:06 120 查看
This problem is purely to test your coding ability. Just go ahead :-)

class Solution {
public:
string countAndSay(int n) {
string num = "1";
for (int i = 1; i < n; i++)
num = say(num);
return num;
}
private:
string say(string& num) {
string s;
int n = num.length(), i = 0, j, k;
for (int i = 0; i < n; i = j, k = 0) {
k = 1, j = i + 1;
while (j < n && num[j] == num[i]) j++, k++;
s += char(k + '0');
s += num[i];
}
return s;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: