leetcode count and say
来源:互联网 发布:lol挂机辅助软件 编辑:程序博客网 时间:2024/05/21 02:48
问题描述:
https://oj.leetcode.com/problems/count-and-say/ 点击打开链接
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.
问题分析:
通过最当前数字序列cur数数, 产生下一个序列next;
1(1个1) --> 11(2个1)--> 21--> 1211
对于输入n, 需要迭代 n - 1 次得到所需要的序列,序列产生的方法
如上边所示。重点在于统计字符s[i]出现的次数,重新拼接成新的字符串。
示例代码:
string countAndSay(int n) { string cur = "1"; for (int i = 1; i < n; i++) { int m = cur.length(); int num = 1; char ch = cur[0]; stringstream next; /* 采用字符串流进行辅助字符串拼组 */ for (int j = 1; j < m; j++) { if (cur[j] == ch) { num++; } /* 统计计数 */ else { next << num << ch; /* 遇到新字符,旧字符统计完成,开始拼接 */ ch = cur[j]; num = 1; } } next << num << ch; cur = next.str(); } return cur; }
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
- ACM-签到 小学题
- twisted入门教程之一:Twisted理论基础
- POJ3177 Redundant Paths (双联通缩点)
- 一些重要知识
- twisted入门教程之二:异步编程初探与reactor模式
- leetcode count and say
- HTTP Charset
- javascript 打印函数名称和被引用的函数
- java并发包的CAS操作
- Pull解析XML格式数据
- 屏幕解锁
- WebServices测试之一-----UFT测试WEBSERVICES
- LCOJ - Intersection of Two Linked Lists
- 递归按层遍历二叉树算法