题目链接

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. Blackfin DSP(五):BF533的SPI接口

    533SPI的特性 最高速度可达SCLK/4: 支持主模式和从模式: 可使用8个GPIO口作为从选择线: 1 slave select input pins 7 slave select output ...

  2. 如何定义DATATABLE,同时赋值

    //定义一个Table DataTable dt=new DataTable("yeji"); DataRow dr; DataColumn dc; //添加第0列 dc=new ...

  3. display:none与visibility: hidden的区别

    display:none和visibility: hidden都能把网页上某个元素隐藏起来,但两者有区别: display:none ---不为被隐藏的对象保留其物理空间,即该对象在页面上彻底消失. ...

  4. PDF 生成插件 flying saucer 和 iText

    最近的项目中遇到了需求,用户在页面点击下载,将页面以PDF格式下载完成供用户浏览,所以上网找了下实现方案. 在Java世界,要想生成PDF,方案不少,所以简单做一个小结吧. 在此之前,先来勾画一下我心 ...

  5. codeforces 361 D - Friends and Subsequences

    原题: Description Mike and !Mike are old childhood rivals, they are opposite in everything they do, ex ...

  6. LINUX内核分析期末总结

    韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.课程总结 1 ...

  7. JAVA学习<四>

    一. 数组: Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  数据类型[ ] 数组名: 或者   数据类型 数组名[ ]: 其中,数组名可以是任意合法的变量名. 2. 分配空间 简单地 ...

  8. Selenium2+python自动化1-环境搭建

    前言 目前selenium版本已经升级到3.0了,网上的大部分教程是基于2.0写的,所以在学习前先要弄清楚版本号,这点非常重要.本系列依然以selenium2为基础,目前selenium3坑比较多,暂 ...

  9. URLEncoder编码

    客户端在进行网页请求的时候,网址中可能会包含非ASCII码形式的内容,比如中文. 而直接把中文放到网址中请求是不允许的,所以需要用URLEncoder编码地址, 将网址中的非ASCII码内容转换成可以 ...

  10. RGBA 与opacity

    RGBA是一种表示颜色的方式,初次看到觉得很奇怪,与RGB的区别是什么?后面查了下,才发现RGBA的好处. RGBA各个字母的含义为: R:红色值,正整数 | 百分数: G:绿色值,正整数 | 百分数 ...