题目链接

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. JAVA设计模式之享元模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述享元(Flyweight)模式的: Flyweight在拳击比赛中指最轻量级,即“蝇量级”或“雨量级”,这里选择使用“享元模式”的意译,是 ...

  2. 2016-08-15:从YUV420P中提取指定大小区域

    typedef struct { int width; int height; }SizeInfo; typedef struct { int x; int y; int width; int hei ...

  3. 你需要知道的swift必备函数 map

    map这东西在oc中并未用过,但是swift在处理数组的时候显得格外的游刃有余,这归功于map这个函数: map函数  arr.map(<#T##transform: (Int) throws ...

  4. highcharts相关属性

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. Egret版本更新(H5增加版本号)

    由于浏览器缓存问题.在服务器上更新了新的图片等资源以后,客户端并不会下载最新的. 关于浏览器缓存机制,可自行百度. Egret中资源更新解决方案有以下: 一 资源名后增加版本号 二 重写Egret引擎 ...

  6. ComponentCount 与 ControlCount 区别

       ShowMessage(panel.ComponentCount.ToString);  ShowMessage(panel.ControlCount.ToString);componetcou ...

  7. jafka的zk数据

    查看topics: ls /brokers/topics [mytopic] 查看topic所在的broker,下面例子,mytopic在broker 0 中管理. ls /brokers/topic ...

  8. window date type

    Most string operations can use the same logic for Unicode and for Windows code pages. The only diffe ...

  9. Linux驱动框架之framebuffer驱动框架

    1.什么是framebuffer? (1)framebuffer帧缓冲(一屏幕数据)(简称fb)是linux内核中虚拟出的一个设备,framebuffer向应用层提供一个统一标准接口的显示设备.帧缓冲 ...

  10. 上传文件(单文件)(FormData)(前端代码+.NET服务器端)

    由于样式需要不能直接用file,只能用文本框+按钮 <form class="form-horizontal form-bordered form-row-strippe" ...