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. Qt编写密钥生成器+使用demo(开源)

    在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制.1:远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点 ...

  2. sencha touch 坑爹的Panel,数据不显示了...

    一位同学问我一个问题: sencha touch中xtype创建dataview死活不显示!!版本2.3.1,MVC模式,sencha touch创建目录程序很简单,主界面一个tabPanel,两个分 ...

  3. 调用TerminateProcess是无法触发DLL_PROCESS_DETACH的

    当应用程序中调用TerminateProcess函数,对于在DllMain函数中处理DLL_PROCESS_DETACH的额外代码操作是无法被执行的.比如:释放资源.数据持久化等.

  4. 使用docker搭建公司redmine服务器

    What is Redmine? Redmine is a flexible project management web application. Written using the Ruby on ...

  5. 【BZOJ4036】[HAOI2015]按位或 FWT

    [BZOJ4036][HAOI2015]按位或 Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal的or ...

  6. jmeter函数助手之time函数实操

    在一个接口测试中,需要提交的请求中要带时间,在看完jmeter帮忙文档,正好总结一下 1.需求 在一个XML请求中请求数据要带有时间,如下 "><ID>/lte/pdeta ...

  7. 近期在看的一段JS(谁能看出我想实现什么功能)

    示例代码: <script type="text/javascript"> !function(){ var e=/([http|https]:\/\/[a-zA-Z0 ...

  8. 【转载】纵观RTX51

    对于使用RTX51的具体好处可以在实践中去体会,就象会用了C51,就不想再用汇编了.用了RTX51,说不定就感到再也离不开它了. 1.RTX51是实时多任务操作系统RTX51是一种实时操作系统既目前在 ...

  9. [分布式系统学习] 6.824 LEC2 RPC和线程 笔记

    6.824的课程通常是在课前让你做一些准备.一般来说是先读一篇论文,然后请你提一个问题,再请你回答一个问题.然后上课,然后布置Lab. 第二课的准备-Crawler 第二课的准备不是论文,是让你实现G ...

  10. ICSharpCode.SharpZipLib.dll 压缩多文件

    网站:http://icsharpcode.github.io/SharpZipLib/ 引用:ICSharpCode.SharpZipLib.dll private string CompassZi ...