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. 【Phalapi2.0】 如何使用 source 通过 header 传参数

    做接口服务时候.有些场景会使用header 来传递参数. 查看官网文档说明 数据来源 source指定当前单个参数的数据来源,可以是post.get.cookie.server.request.hea ...

  2. VC++中如何复制对话框资源

    法1:   在你的工程中添加另一个工程的rc文件,这时资源视图中就会出现两个rc,从后加的rc中拷贝资源到你自己工程的rc中就可以了.       法2:vc中如何拷贝一个工程的对话框资源到另一个工程 ...

  3. MacOS 安装PyQt5

    PyQt5官方安装教程指出2种安装方法: Installing from Wheels Building and Installing from Source 网上搜罗的大多是按照第二种方法安装的,本 ...

  4. Windows Phone 8.1 Update Preview backup失败的解决方法

    当前使用的是开发者预览版(8.10.14219.341),最近一直无法备份text + apps + settings,进度条达到97%或99%以后提示: There was a problem ba ...

  5. Xcode - LLDB调试技巧

    LLDB是Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍. ...

  6. 自定义tarBar

    使用tarBar大多数情况在我们都是默认的tarBarButton尺寸和位置但是如果我们想,希望像新浪微博那样的tarBar,就需要自定义了. 1.本质上其实就是通过我们的主控制器中以KVC的方式重新 ...

  7. python添加Windows环境变量

    1.cmd中添加方式 SET PATH=%PATH%;c:\Program Files (x86)\Wireshark 注:如上代码添加c:\Program Files (x86)\Wireshark ...

  8. HDFS文件系统的JAVA-API操作(一)

    使用java.net.URL访问HDFS文件系统 HDFS的API使用说明: 1.如果要访问HDFS,HDFS客户端必须有一份HDFS的配置文件 也就是hdfs-site.xml,从而读取Nameno ...

  9. Java编程思想中关于闭包的一个例子

    Java编程思想中的一个例子,不是很理解使用闭包的必要性,如果不使用闭包,是不是有些任务就不能完成?继续探索. package InnerClass; interface Incrementable ...

  10. linker command failed with exit code 1 (use -v to see invocation) 变量重名

    有时候,xcode报错看不到,点最后一个按钮,类似气泡的就能看到 报错信息: duplicate symbol _imgNummmm in:    /Users/mianmian/Library/De ...