LeetCode38——Count and Say
2015-02-11 18:43
429 查看
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
容易
[code]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.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
难度系数:
容易
实现
[code]string countAndSay(int n) { if (n == 1) { return string("1"); } string prev = countAndSay(n-1); int before = 0; int count = 0; string curr; for (int i = 0; i < prev.length(); ++i) { if (prev[i]-'0' == before) { count++; continue; } else if (before != 0) { curr.append(1, '0' + count); curr.append(1, '0' + before); } before = prev[i]-'0'; count = 1; } if (before != 0 && count != 0) { curr.append(1, '0' + count); curr.append(1, '0' + before); } return curr; }
相关文章推荐
- Leetcode 38 Count and Say 传说中的递推
- LeetCode_38---Count and Say
- LeetCode 38 Count and Say(字符串生成)
- leetcode 38:count and say
- leetcode-38 Count and Say
- 【LeetCode】38 - Count and Say
- leetcode 38: Count and Say
- leetcode 38:Count and Say
- leetcode_question_38 Count and Say
- LeetCode 38:Count and Say
- leetcode-38-count and say
- leetcode-38-Count and Say 基础题
- [Leetcode]_38 Count and Say
- LeetCode-38-Count and Say(计数并显示)
- Leetcode 38,Count and say
- LeetCode 38 : Count and Say (Java)
- LeetCode 38 Count and Say(计数与报数)
- LeetCode(38) Count and Say
- 【LeetCode 38_字符串_算术运算】Count and Say
- LeetCode 38 Count and Say(C,C++,Java,Python)