[LeetCode] Count and Say
来源:互联网 发布:java guava cache 编辑:程序博客网 时间:2024/05/29 07:51
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
.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
class Solution {public: string countAndSay(int n) { string s = "1"; string ans; if(n == 1){ return s; } for(int i = 1,j; i < n;i ++){ j = 0; while(j < s.size()){ int k = j + 1; while(k < s.size() && s[k] == s[j]) k ++; ans += (k - j + '0'); ans += s[j]; j = k; } s = ans; ans = ""; } return s; }};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- 世界阻燃剂前景分析
- codeforce 277.5 A
- poj.1008
- LeetCode OJ 之 Populating Next Right Pointers in Each Node (为每个结点填充右指针)
- 素数
- [LeetCode] Count and Say
- C#总结
- 软件工程阶段性总结(四)——测试和维护
- IDL 函数MIN_CURVE_SURF详细说明
- linux僵死进程
- 如何对一个算法进行复杂度分析
- Python 之 decorator 装饰器
- HDU 2561 第二小整数
- 银行家算法学习笔记