The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2. 11
3. 21
4. 1211
5. 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 term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"

Example 2:

Input: 4
Output: "1211" 1. Constraints
1)n >= 1 edge case n == 1: return '1' 2. Ideas
recursive helper() , helper()是一个能count and say 之前的string 3. Code
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
def helper(n, s):
if n == 1: return s
pre, i , temp, count = s[0], 0, "", 0
while i < len(s):
if s[i] == pre:
count += 1
else:
temp += str(count) + pre
count = 1
pre = s[i]
i += 1
if i == len(s):
temp += str(count) +pre
return helper(n-1, temp)
ans = ""
return helper(n, '')

[LeetCode] 38. Count and Say_Easy的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  3. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  4. Java [leetcode 38]Count and Say

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

  5. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  6. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  7. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  8. LeetCode - 38. Count and Say(36ms)

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  9. LeetCode题解38.Count and Say

    38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...

随机推荐

  1. 纯css制作带三角(兼容所有浏览器)

    如何用 border 来制作三角. html 代码如下: 代码如下: <div class="arrow-up"></div> <div class= ...

  2. Github for Windows 登录时报代理问题?

    Github for Windows 登录时报如下错误: 不要被它的提示信息误导了. 登录失败,跟代理半毛钱关系都没有. 是 .net framework 组件 的问题. 更新下 .net frame ...

  3. icon VS html特殊字符

    好久没来了,最近项目很多,今天要说的是个页面上用到的icon. 话“icon” 现在有很多icon库,我们再也不用切图来适配不同的分辨率了,但是对于新手来说,查阅icon库来找到适合的icon,实在费 ...

  4. SharpGL学习笔记(六) 裁剪变换

    在OpenGL中,除了视景体定义的6个裁剪平面(上下左右前后)外, 用户还可以定义一个或者多个附加的裁剪平面,以去掉场景中无关的目标. 附加平面裁剪函数原型如下: ClipPlane(OpenGL.G ...

  5. Centos重新启动网络配置文件,/etc/resolv.conf被覆盖或清空问题解决

    Centos在执行命令 yum update时报错如下: Could not get metalink https://mirrors.fedoraproject.org/metalink?repo= ...

  6. goldengate 过滤对某张表的复制操作

    在复制进程中配置下面的参数可以实现对一个用户下的某些表进行过滤,在复制的时候 不做任何操作. MAPEXCLUDE: Valid for Replicat Use the MAPEXCLUDE par ...

  7. js ==和===以及!= 和 !==的区别

    一.js == 与 === 的区别[转] 1. 对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型 ...

  8. ubuntu安装vncserver实现图形化访问

    请注意: 如果在安装中部分软件无法安装成功,说明软件源中缺包,先尝试使用命令#apt-get update更新软件源后尝试安装.如果还是不行,需要更换软件源.更换步骤: a)输入命令#cp /etc/ ...

  9. 【CF720D】Slalom 扫描线+线段树

    [CF720D]Slalom 题意:一个n*m的网格,其中有k个矩形障碍,保证这些障碍不重叠.问你从(1,1)走到(n,m),每步只能往右或往上走,不经过任何障碍的方案数.两种方案被视为不同,当且仅当 ...

  10. iOS - viewDidLoad, viewWillDisappear, viewWillAppear区别及加载顺序

    viewWillAppear: Called when the view is about to made visible. Default does nothing视图即将可见时调用.默认情况下不执 ...