count and say
2015-09-21 15:58
281 查看
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
给定一个整数
您在真实的面试中是否遇到过这个题?
Yes
样例
给定 n =
注意
整数的顺序将表示为一个字符串。
1, 11, 21, 1211, 111221, ...
1读作
"one 1"->
11.
11读作
"two 1s"->
21.
21读作
"one 2, then one 1"->
1211.
给定一个整数
n, 返回 第
n个顺序。
您在真实的面试中是否遇到过这个题?
Yes
样例
给定 n =
5, 返回
"111221".
注意
整数的顺序将表示为一个字符串。
class Solution { public: /** * @param n the nth * @return the nth sequence */ string countAndSay(int n) { // Write your code here if(n==1) return "1"; if(n==2) return "11"; string pre=countAndSay(n-1); int nn=pre.size(); string res; int count=1; for(int i=1;i<nn;i++){ if(pre[i]==pre[i-1]) count++; else{ res+=to_string(count)+pre[i-1]; count=1; } } res+=to_string(count)+pre[nn-1]; return res; } };
相关文章推荐
- MTK8127 把系统的apk不编译进入system.img
- request.getHeader()
- 中国 省市区县 数据库SQL 脚本(含邮编)
- redis修改的源代码zincrby,hincrby命令
- emmet使用笔记及sublime常用快捷键
- TextToSpeech之阅读文字
- 框架学习八:二维码(Zxing)
- oracle10G 数据泵技术
- spark作业调度
- @property
- Java中如何实现时间的加减
- Torch基础学习(一)
- Boost和标准库中正则表达式的使用
- sql去重查询
- hdu 1257 最少拦截系统 (贪心)
- 添加的千分的分隔符
- Tomcat下部署多个项目
- ocp-v13-683
- 添加千分的分隔符
- git 命令学习及更新