LeetCode:Count and Say
2015-11-24 16:44
405 查看
Count and Say
My SubmissionsQuestion
Total Accepted: 62217 Total
Submissions: 233796 Difficulty: Easy
The count-and-say sequence is the sequence of integers beginning as follows:
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.
题意:
第1个数:1
第2个数:从前一个数来,上一个数是1个1,故:11
第3个数:上一个数是2个1,故:21
第4个数:上一个数是1个2 + 1个1,故:1211
...
求第n个数。
code:
class Solution { public: string countAndSay(int n) { string s[2]; s[0]="1"; s[1]=""; int cur=0; for(int i=1;i<n;i++) { int cnt; for(int j=0;j<s[cur].size();j+=cnt) { cnt=0; char t=s[cur][j]; for(int k=j;k<s[cur].size() && t==s[cur][k];k++) cnt++; s[cur^1]+='0'+cnt; s[cur^1]+=t; } s[cur]=""; cur^=1; } return s[cur]; } };
相关文章推荐
- MyBatis增删改查
- 关于HADOOP HA 中DFSZKFC的理解
- 212
- Eclipse HibernateTools安装
- 海尔电脑U盘重装系统win7步骤图文教程
- 推进存储池化!浪潮推出业界首款基于SAS协议的交换式存储池(基于PMC技术)
- JSON 之 SuperObject(1)
- 通过 trace文件 备份控制文件controlfile
- Android apk反编译
- 关于AndroidAnnotations取消后台任务的方法
- 浏览器判断兼容IE
- cocosjs 触摸
- MapReduce编程实例之数据去重
- java.lang.noclassdeffounderror:com/mchange/v2/ser/Indirector
- gifflen 在出现 b = (((*p)) & 0xff) << netbiasshift; 问题时的解决办法
- MQTT连接服务器发送和接收数据
- map解析的几种方式
- Oracle 统计信息(备份/导入)表分析-[注意要刷缓存]
- 消息队列
- 动态生成DropDownList 并取值