#38. Count and Say
来源:互联网 发布:阿里云及产品体系介绍 编辑:程序博客网 时间:2024/06/10 23:34
题目描述:
The count-and-say sequence is the sequence of integers with the first five terms as following:
- 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 term of the count-and-say sequence.Note: Each term of the sequence of integers will be represented as a string.
找规律模拟的题,考逻辑。
class Solution {public: string countAndSay(int n) { if(n==1) return "1"; else return iteration("1",1,n); } string iteration(string s,int k,int n){ //cout<<"k = "<<k<<" "<<"s = "<<s<<endl; if(k==n) return s; string result; int i,j; for(i=0;i<s.size();){ j=i; while(j<s.size()&&s[i]==s[j]) j++; result.push_back(j-i+'0'); result.push_back(s[i]); i=j; //cout<<"result = "<<result<<endl; } return iteration(result,k+1,n); }};
本身是一个迭代的过程
阅读全文
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- java线程详解
- Linus实验楼笔记——第13节挑战:历史命令统计
- Sql Server for linux 安装
- html浏览器默认样式清楚
- for循环执行过程验证
- #38. Count and Say
- Python User-Agent列表
- 每日一题--把数组排成最小的数
- Python日期时间函数
- 转一篇写的不错的NSIS打包脚本示例
- 【Hystrix系列】一、配置加载过程分析
- 检索算法 ---顺序查找
- linux 上的SQl Server 的图形界面管理软件
- 新的表格展示利器 Bootstrap Table Ⅱ