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. ARM汇编 均值滤波实验

    实验要求是排序后去掉最大值最小值,然后把剩下的求平均数. 排序可以用之前的冒泡排序,关键的问题是求平均数.因为ARM没有除法,应该怎么求平均数呢? 最简单的方法就是减法了,用被除数一直减除数,看减了多 ...

  2. 使用ExitProcess()结束本进程、TerminateProcess 结束进程

    进程只是提供了一段地址空间和内核对象,其运行时通过在其地址空间内的主线程来体现的.当主线程的进入点函数返回时,进程也就随之结束.这种进程的终止方式是进程的正常退出,进程中的所有线程资源都能够得到正确的 ...

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

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

  4. svn 的限制

    *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo *.rej *~ #*# .#* .*.swp .DS_Store *.exe *. ...

  5. #define中 #与##的神奇用法

    本文整理自csdn. #define f(a,b) a##b  #define d(a) #a  #define s(a) d(a)  void main( void )  {      puts(d ...

  6. zoj1037-Gridland

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=37 Gridland Time Limit: 2 Seconds      Me ...

  7. poj1067-取石子游戏 (威佐夫博弈)

    http://poj.org/problem?id=1067 取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36 ...

  8. Python_11-正则表达式

    目录: 1.1      引言 1.2      python 正则式概述及常用字符 1.2.1       元字符 1.2.2       用 "" 开始的特殊字符所表示的预定义 ...

  9. NPC问题及其解决方法(回溯法、动态规划、贪心法、深度优先遍历)

    NP问题(Non-deterministic Polynomial ):多项式复杂程度的非确定性问题,这些问题无法根据公式直接地计算出来.比如,找大质数的问题(有没有一个公式,你一套公式,就可以一步步 ...

  10. SpringBoot29 登录逻辑、登录状态判断

    1 知识点扫盲 浏览器和服务器之间时通过session来确定连接状态的,浏览器第一次请求时服务端会自动生成一个session,并将这个sessionId传回给浏览器,浏览器将这个sessionId存放 ...