[LeetCode] 38. Count and Say_Easy
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的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
- 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 ...
- LeetCode题解38.Count and Say
38. Count and Say The count-and-say sequence is the sequence of integers beginning as follows: 1, 11 ...
随机推荐
- 【python3】urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>
在玩爬虫的时候,针对https ,需要单独处理.不然就会报错: 解决办法:引入 ssl 模块即可 核心代码 imort ssl ssl._create_default_https_context = ...
- android checkbox样式
1. 首先要导入你准备用作CheckBox选中和补选中状态的两图片到res的drawable中,如checkbox_checked.png,checkbox_normal.png: 2. 在res/d ...
- G711算法学习
采样和量化 首先需要明确的两个概念,“采样”和“量化”.对于给定的一个波形,采样是从时间上将连续变成离散的过程,而采样得到的值,可能还是不能够用给定的位宽(比如8bit)来表示,这就需要经过量化,即从 ...
- git查看指令
打开git bash 1,查看自己之前是否生成过ssh密钥 $ ls .ssh 如果存在这个id_rsa.pub这个文件的话表示已经生成了 2,查看用户名和邮箱 $ git config --glob ...
- C# .ToString()格式化 常用数据转化小总结
1.百分比 ; ; string p = ((double)i / j).ToString("P");//结果:200.00% p = string.Format("{0 ...
- 关于VMware虚拟机磁盘收缩的几种方法
VMware虚拟机在使用过程中,随着软件和数据的增多,虚拟磁盘占用的硬盘空间会逐渐增大,但删除数据后,却不会自动减小占用的物理硬盘空间 而是继续占用相应大小.如果需要解决上面的问题,就需要收缩wmwa ...
- Unity3D笔记 Collect
一.输入轴 默认输入轴: Horizontal 和 Vertical被映射到w, a, s, d键和方向键 Fire1, Fire2, Fire3被分别映射到Ctrl,Option(Alt)和Comm ...
- jfinal如何设置使用哪种模板引擎(视图)
1.jfinal\com\jfinal\core\Controller.java /** * Render with view use default type Render configured i ...
- linux启动程序和关闭程序脚本
关闭脚本: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log ...
- Office2007 每次打开斗需要检查 【配置进度】
打开C:\Program Files\Common Files\microsoft shared\OFFICE12\Office Setup Controller目录下将SETUP.EXE 重命名为 ...