LeetCode-Count and Say
2013-08-03 20:55
225 查看
class Solution { public: string countAndSay(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function string str = "1"; string strN = ""; char buf[100]; for (int i = 0; i < n - 1; ++i) { int cnt = 1; int j; for (j = 1; j < str.size(); ++j) { if (str[j] == str[j - 1]) { ++cnt; } else { //这里要特别注意不能直接strN += cnt + '0',因为strN有可能不是一位数 itoa(cnt, buf); strN += buf; strN += str[j - 1]; cnt = 1; } } itoa(cnt, buf); strN += buf; strN += str[j - 1]; str = strN; strN = ""; } return str; } void itoa(int i, char *a) { int j = 0; while (i != 0) { int tmp = i % 10; a[j++] = tmp + '0'; i /= 10; } a[j] = '\0'; reverse(a, a + j); } };
相关文章推荐
- leetcode-38 Count And Say
- [LeetCode] Count and Say
- [Leetcode]38--Count and Say
- Count and Say --leetcode
- [leetcode]Count and Say
- [C++]LeetCode: 20 Count and Say
- (leetCode)Count and Say --- 统计读
- 【LeetCode】38 - Count and Say
- leetcode 题解 || Count and Say 问题
- Leetcode: Count and Say
- 【leetcode】Count and Say
- 【leetcode】Count and Say (easy)
- Leetcode 38 Count and Say 传说中的递推
- leetcode 038 Count and Say
- [LeetCode] Count and Say
- Leetcode 38 Count and Say
- **(leetcode_string) Count and Say
- 开始刷leetcode day30:Count and Say
- LeetCode-Count and Say
- Leetcode38:count and say数一数,说一说