您的位置:首页 > 编程语言 > C语言/C++

leetcode_c++:Count and Say(038)

2016-08-06 14:28 429 查看
The count-and-say sequence is the sequence of integers beginning as follows:

1, 11, 21, 1211, 111221, …

1 is read off as “one 1” or 11.

11 is read off as “two 1s” or 21.

21 is read off as “one 2, then one 1” or 1211.

o(n^2)

class Solution {
public:
string countAndSay(int n) {
string pres = "1", news;
char tmpch;
int len, count;
for (int i = 1; i < n; i++) {
news = "";
len = pres.length();
tmpch = pres[0];
count = 1;
for (int i = 1; i < len; i++) {
if (tmpch == pres[i])
count++;
else {
news += count + '0';
news += tmpch;
tmpch = pres[i];
count = 1;
}
}
// the last
news += count + '0';
news += tmpch;
pres = news;
// cout << pres << endl;
}
return pres;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: