LeetCode|Count and Say
2016-05-14 22:23
225 查看
Count and Say
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) { if(n == 0) return ""; string res = "1"; for(int i = 2; i <= n; i++){ res = help(res); } return res; } private: string help(string str){ stringstream ss; str += '\0'; // add a terminator char last = str[0]; int cnt = 0; for(int i = 0; i < str.length(); i++){ if(str[i] == last){ cnt++; } else{ ss << cnt << last; cnt = 1; last = str[i]; } } return ss.str(); } };
相关文章推荐
- MySQL之——安装时出现APPLY security settings错误
- 用STM32自制鼠标
- R语言绘制文字云
- amazon: 大于非负整数N的第一个回文数
- [Linux]安装phpredis扩展
- python入门(一)
- 144. Binary Tree Preorder Traversal
- HDU 5682 zxa and leaf
- 北京市工作居住证官方网站js报错不能使用解决办法
- OpenGL 解析
- CCF201312-1
- MFC对话框如何运行后隐藏起来
- 第十。十一周项目3 - 警察和厨师——1
- RecyclerView通用适配器
- 钱咖助手-好玩的手机平台-免费开发尝试2
- 3背包问题分ab
- 离职总结:大公司与小公司的个人体验
- C++走向远洋——48(项目一1、复数类中的运算符重载、类的成员函数)
- Java模式之责任链模式
- 17907955 2016-05-14 17:17:32 biss A - Summer Camp GNU C++11 Accepted 15 ms 2200 KB