Count and Say-LeetCode
来源:互联网 发布:软件测试书籍 知乎 编辑:程序博客网 时间:2024/06/16 07:29
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.
用到了一个平时没有用到的stringstream,用来将int转化为string,十分便捷,使用方法见下面的博文链接。
http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html
代码:
class Solution {public: string getnextstr(string str) { stringstream ss; char key = str[0]; char current = str[0]; int count = 0; for (int i = 0; i < str.length(); i++) { current = str[i]; if (key == current) { count++; } else { ss<<count<<key; key = current; count = 1; } } ss<<count<<key; return ss.str(); } string countAndSay(int n) { string result = "1"; if ( n <= 0) { return NULL; } if (n == 1) { return result; } for (int i = 0; i < n - 1; i++) { result = getnextstr(result); } return result; }};
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
- 小黑小波比.判断数组中是否含有一个元素
- Eclipse Plugin机制中的扩展点
- Java 单例模式
- arrayList
- 更新Yosemite后MySQL无法启动
- Count and Say-LeetCode
- Android Studio导入第三方类库的方法
- mac与windows10共享互传用于访问的用户名需要用英文!!!
- 防火墙打开远程连接SQL Server端口
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- Lucene 4.7 教程起步--创建索引
- myBatis批量添加,修改和删除
- 互联网界神话——永远不要小看任何一个人
- jvm的参数含义及设置