lintcode 报数
来源:互联网 发布:js编辑器 编辑:程序博客网 时间:2024/05/25 18:12
报数
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
1, 11, 21, 1211, 111221, ...
1
读作 "one 1"
-> 11
.
11
读作 "two 1s"
-> 21
.
21
读作 "one 2, then one 1"
-> 1211
.
给定一个整数 n
, 返回 第 n
个顺序。
注意事项
整数的顺序将表示为一个字符串。
样例
给定 n = 5
, 返回 "111221"
.
class Solution {public: /* * @param n: the nth * @return: the nth sequence */ string countAndSay(int n) { // write your code here string s="1"; if(n==1) return s; while(--n){ string tmp=""; int count=1; for(int i=1;i<s.size();i++){ if(s[i]==s[i-1]){ count++; } else { tmp+=to_string(count)+s[i-1];////to_string int转string;//这里总是加上一个数,所以最后的一个数要额外补上 count=1; } } tmp+=to_string(count)+s.back();//最后的字符串加上,back()返回string的随后一个字符串 s=tmp; } return s; }};
阅读全文
0 0
- LintCode:报数
- lintcode ----报数
- 报数-lintcode
- lintcode,报数
- lintcode--报数
- 报数-LintCode
- lintcode 报数
- 报数(LintCode)
- LintCode之报数
- 容易 报数 (lintcode)
- lintCode(420)——报数
- lintcode python代码 420 报数
- LintCode 第420题 报数
- lintcode 报数(Count and Say) (Java)
- 报数
- 报数
- 报数
- 报数
- Verilog HDL语言的使用
- 30天了解30种技术系列---(13)Docker UI框架DockerUI
- CentOS配置YUM实现安装MySQL5.7
- ORB-SLAM(一)简介
- centos7配置eclipse+maven+tomcat+mysql步骤
- lintcode 报数
- maven的下载和安装(win和IOS)
- python中的原始字符串,神奇的反斜线
- Spring容器对Bean的管理
- java日常学习:单例设计模式
- log4j.xml 记录
- 22岁ISFJ职业规划,“我想做中产阶级,这可如何是好?”
- Unidirectional TSP UVA
- 1010