Count and Say
来源:互联网 发布:知乎 上知网 编辑:程序博客网 时间:2024/06/10 19:50
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.
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"; for(int i=1;i!=n;++i){ s = countAns(s); } return s; } string countAns(string s){ int k =s.size(); char ch = s[0]; int count = 0; string ss; for(int i = 0;i<k;i++){ if(ch==s[i]){ count++; }else{ ss.push_back('0'+count); ss.push_back(ch); count = 1; ch = s[i]; } } ss.push_back('0'+count); ss.push_back(ch); return ss; }};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- Count and Say
- [38]Count and Say
- Count and Say
- Count and Say
- Count and Say
- [LeetCode]Count and Say
- Count and Say
- Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- android 4.4 支持透明状态栏和透明导航栏
- easyui的引入我所犯的错误改正
- 编写优质嵌入式C程序
- CreateMutex
- Android之SurfaceView(二)
- Count and Say
- Quartz任务调度快速入门
- 苹果开发 笔记(19)
- ExpandableListView讲解
- 正则表达式
- OS 的一些指标信息
- 设计一个有理数类,应该包含加,减,乘,除运算。
- tcpdump命令介绍
- 构建webx简单应用