LeetCode题解——Count and Say
2015-08-31 16:43
375 查看
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
1, 11, 21, 1211, 111221, ...
1is read off as
"one 1"or
11.
11is read off as
"two 1s"or
21.
21is 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"; int count = 1; while(count++ < n){ string t; for(int i=0;i<s.size();i++){ int c=1; while(i<s.size()-1 && s[i]==s[i+1] ) {i++;c++;} t+=(c+'0'); t+=s[i]; } s=t; } return s; } };
相关文章推荐
- hihoCoder 1224 : 赛车
- 使用 Sahi 实现 Web 自动化测试
- hdoj 4062 Queuing 【矩阵快速幂优化递推公式】
- android 轻量级的友好的交互对话框库,EasyDialog,实现已详细标注
- 《Data Structures and Algorithm Analysis in Java》Second Edition中文版笔记
- (Android系列之Fragment(一)----Fragment加载到Activity当中)
- 对Android初学者的10个建议
- Darwin Streaming Server windows系统下安装和配置
- Java- 受检的异常(checked Exception)
- java.sql.SQLException: Access denied for user 'root'
- 设计模式(1)--简单工厂模式、策略模式
- Quartz.Net 作业调度后台管理系统,基于Extjs
- 层序输出二叉树:
- Android5.0 铃声免打扰设置源码浅析
- 【linux】centos7网络配置
- POI 读取 Excel 文件(2003版本与2007版本的差异之处)
- iOS-监测网络状态
- 发送邮件代码
- Map
- yum安装ftp服务器