题目链接

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.

题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。

http://blog.csdn.net/kenden23/article/details/17081853

https://github.com/krystism/leetcode/tree/master/algorithms/CountandSay

http://www.cnblogs.com/huxiao-tee/p/4109596.html

注意:用n(int)+'0'表示'n',仅当n<10时有效!

class Solution {
public:
string countAndSay(int n) {
if(==n)
return NULL;
string result="";
while(--n){
result=nextSequence(result);
}
return result;
}
private:
string nextSequence(string s1){
char cur=s1[];
int count=;
string result;
for(int i=;i<s1.size();i++){
if(cur!=s1[i]){
result+=itos(count);
result.push_back(cur);
cur=s1[i];
count=;
}
else{
count++;
}
}
result+=itos(count);
result.push_back(cur);
return result;
}
string itos(int i){
ss.str("");
ss.clear();
ss<<i;
return ss.str();
}
stringstream ss;
};

Count and Say leetcode的更多相关文章

  1. Count and Say leetcode java

    题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...

  2. 696. Count Binary Substrings - LeetCode

    Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...

  3. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. LeetCode Patching Array

    原题链接在这里:https://leetcode.com/problems/patching-array/ 题目: Given a sorted positive integer array nums ...

  6. leetcode math类型题目解题总结

    2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution { publi ...

  7. LeetCode哈希表

    1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...

  8. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  9. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

随机推荐

  1. UIUC同学Jia-Bin Huang收集的计算机视觉代码合集

    转自:http://blog.sina.com.cn/s/blog_631a4cc40100wrvz.html   UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: ...

  2. review过去的10年

    本科毕业有10个年头多了,如果对我的博客做一个主题分析,还真能发现一些规律,这里总结一下: 1.  活跃度 本科毕业最后一学期是思维最活跃的阶段,人生面临很多的变化和挑战,心态相对还不错. 从来北京以 ...

  3. Spark Streaming源码解读之生成全生命周期彻底研究与思考

    本期内容 : DStream与RDD关系彻底研究 Streaming中RDD的生成彻底研究 问题的提出 : 1. RDD是怎么生成的,依靠什么生成 2.执行时是否与Spark Core上的RDD执行有 ...

  4. openldap加密传输sssd

    http://blog.father.gedow.net/2015/09/29/sssd-ldap-sudo/ yum -y install openldap-clients sssd authcon ...

  5. jQuery 中 data 方法的实现原理

    前言:jQuery 作为前端使用最多最广泛的 JS 库,其源码每个 JSer 都应该研究一下.早就打算看却一直被各种事拖着,上次某公司面试时被问到 jQuery 中 data 方法是如何实现的,结果答 ...

  6. 【Linux】依赖包检查

    参考:http://www.cnblogs.com/zc22/p/3197038.html ldd xx.so

  7. 关键字static和this

    1.this关键字 this:代表当前对象,就是代表所在函数所属类的引用.哪个对象调用了this所在的函数,this就代表哪个对象. this关键字也可以用于在构造函数中调用其他构造函数,此时要注意, ...

  8. 一天天的sql总结

    一. 多张表之间的查询: join/inner join  on inner join 是比较运算符,只返回符合条件的行. left/outer join on 左外连接包含left join左表所有 ...

  9. Android DDMS检测内存泄露

    Android DDMS检测内存泄露 DDMS是Android开发包中自带工具,可以测试app性能,用于发现内存问题. 1.环境搭建 参考之前发的Android测试环境搭建相关文章,这里不再复述: 2 ...

  10. linux 驱动学习笔记02--应用实例:在内核中新增驱动代码目录和子目录

    下面来看一个综合实例,假设我们要在内核源代码 drivers 目录下为 ARM 体系结构新增如下用于 test driver 的树型目录:| --test  | -- cpu  | -- cpu.c ...