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.

本题的难点在于理解题目的意思。

最好的理解就是给出 111221之后的一个sample: 312211

如何理解呢?  将111221 拆成后面的表达式即可: 111, 22, 1 其中有 3个one , 2个two, 1个one ,读起来就是: 312211

 class Solution {
public:
string countAndSay(int n) {
string a="";
if(n==) return a;
for(int i=;i<=n;i++)
{
string b;
char current = a[];
int j=,count=;
for(j=;j<a.size();j++)
{
if(a[j]==current)
{
count++;
}
else
{
char c = count +'';
b+=c;
b+=current;
current = a[j];
count=;
}
}
char c = count+'';
b+=c;
b+=current;
a= b;
}
return a;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢

[LeetCode 题解]: Count and Say的更多相关文章

  1. 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)

    目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...

  2. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  3. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  4. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  5. leetcode题解#3:无重复字符的最长子串

    leetcode题解:无重复字符的最长子串 题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: s = "abcabcbb"输出: 3 解释 ...

  6. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  7. leetcode题解-122买卖股票的最佳时期

    题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...

  8. 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)

    目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

随机推荐

  1. Android 4 学习(21):对话框

    对话框 创建Dialog的两种方式: 1. 使用Dialog类或其子类,包括DialogFragment 2. 在Activity中使用Dialog主题(theme) 下面是使用Dialog类的一个例 ...

  2. halcon连续采集图像

    dev_close_window()dev_update_window('off')create_bar_code_model ([], [], BarCodeHandle)dev_open_wind ...

  3. mongodb主从复制 副本集(六)

    主从复制副本集 8888.conf dbpath = D:\software\MongoDBDATA\07\8888 #主数据库地址port = 8888 #主数据库端口号bind_ip = 127. ...

  4. C++Primer笔记-----day05

    =======================================================================day05======================== ...

  5. sortingOrder,sortingLayer

    sortingOrder 是sortingLayer内的分级 sortingLayer是抽象的分层,用于决定2D物体绘制的先后顺序. 2D物体分两类:sprite和UI. sprite虽是2D,却可以 ...

  6. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  7. JAVA用email.jar发送邮件

    1 jar包 email.jar包,网上下载 2 源代码 package zjr.amy.emil.test; import java.util.Date; import java.util.Prop ...

  8. C#中的goto

    int i = 9;if (i % 2 == 0) goto Found;else goto NoFound; NoFound:            Console.WriteLine(i.ToSt ...

  9. preview放大镜

    [preview放大镜] preview有放大镜功能,放`键即可(键盘左上角,1左边的键).

  10. AddComponentMenu

    [AddComponentMenu] The AddComponentMenu attribute allows you to place a script anywhere in the " ...