Count and Say
来源:互联网 发布:必赢客软件 编辑:程序博客网 时间:2024/06/07 04:54
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=this->itoa(1);//第一个 for(int i=2;i<=n;++i){//从第二个开始 s = nextNum(s); } return s; }private: string itoa(int i){ char buf[10]; memset(buf,0,sizeof(buf)); sprintf(buf,"%d",i); string s(buf); return s; } string nextNum(string &s){ string res; int j=1,count =1; for(;j<s.size();++j){ if(s[j] == s[j-1]) ++count; else{ res.push_back('0'+count); res.push_back(s[j-1]); count=1; } } res.push_back('0'+count); res.push_back(s[j-1]); return res; }};
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
- HDU 2674 N!Again(数论)
- 行人检测(综述2)
- 关于duplicate symbols for architecture i386 问题
- 黑马程序员——JAVA基础---线程之间的通信实现交替输出打印
- android按钮拖动Button 在up事件中做弹出菜单调用(通过判断坐标解决事件冲突问题)
- Count and Say
- Jdom操作xml,按照指定要求分类
- fedora20上搭建MPI集群
- sql server 2008 “重启计算机失败” 和 “远程过程调用失败” 的解决方法
- 小用stm32f4-CAN控制器(使用库函数)
- makefile详解 定义模式规则
- 汽车加油行驶问题(分层图最短路)
- 李碧華 姊妹愛 歌词
- mrc 和 arc的转换