[LeetCode]Count and Say
来源:互联网 发布:银行卡数据采集器 编辑:程序博客网 时间:2024/06/05 01:04
The count-and-say sequence is the sequence of integers beginning as follows: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 sequence.
Note: The sequence of integers will be represented as a string.
对这个序列持续读写,找好规律即可。
class Solution {public: string countAndSay(int n) { if(n==1) return "1"; string ret = "1"; for(int i = 1; i<n; ++i){ ret = nextString(ret); } return ret; } string nextString(string s){ char temp = s[0]; s = s+'a'; //边界点 string ret; int index = 1; for(int i=1; i<s.length(); ++i){ if(s[i]==temp){ index++; } else{ char count = '0'+index; ret = ret+count; ret = ret+temp; index = 1; temp = s[i]; } } return ret; }};
0 0
- LeetCode: Count and Say
- [LeetCode]Count and Say
- LeetCode Count and Say
- [Leetcode] Count and Say
- Leetcode: Count and Say
- [LeetCode] Count and Say
- LeetCode Count and Say
- [LeetCode]Count and Say
- [LeetCode] Count and Say
- LeetCode-Count and Say
- LeetCode: Count and Say
- LeetCode - Count and Say
- [LeetCode] Count and Say
- Count and Say 【leetcode】
- [LeetCode] Count and Say
- Leetcode Count and Say
- LeetCode Count and Say
- LeetCode | Count and Say
- 秒杀核心设计——防止超卖、高并发与varnish详解
- 南昌大学软件学院21天大神养成计划之第一天学习心得
- Launcher3 应用图标的加载流程
- java实现冒泡算法
- 发送短信或邮件
- [LeetCode]Count and Say
- kNN算法 k-Nearest Neighbors algorithm
- 微波工程学习心得(一)D.M.Pozar版
- wxpython学习2
- 学习html5(2)
- 安卓位置服务简介
- SHA算法详解
- UI阶段 protocol/delegate设计模式
- iOS 多图上传