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)
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; } };
相关文章推荐
- Java for LeetCode 038 Count and Say
- leetcode之路038 Count and Say
- LeetCode刷题(C++)——Count and Say(Easy )
- 【leetcode c++】38 Count and Say
- 【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】
- [LeetCode]038-Count And Say
- leetcode 038 Count and Say
- leetcode解题方案--038--count and say
- LeetCode 038 Count and Say
- leetcode 038 Count and Say
- LeetCode038 Count and Say
- [LeetCode]题解(python):038-Count and Say
- LeetCode 038 Count and Say
- [C++]LeetCode: 20 Count and Say
- leetcode 038 —— Count and Say
- LeetCode 038-Count and Say
- LeetCode 038 Count and Say
- leetcode 038 Count and Say
- leetcode 64: Count and Say
- Leetcode Count and Say 数数列数字