mycode   91.28%

思路:题意实在太难理解了,尤其是英文又不好,只能参看下别人的资料,理解下规则。终于理解,题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。

class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
from collections import Counter
if n == :
return ''
a = self.countAndSay(n-) res , temp , count = '' , a[] ,
for i in range(,len(a)):
if a[i] == temp:
count +=
else:
res += str(count) + str(temp)
temp = a[i]
count =
res += str(count) + str(temp)
return res

参考

把递归换成了for循环

class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
x= ''
y=''
prev = ''
for i in range(n-1):
count = 0
y = ''
prev = x[0]
for j in x: if prev==j:
count+=1 else:
y+=(str(count)+str(prev))
prev = j
count = 1 y+= (str(count)+str(prev))
print('y: ',y)
x = y return(x)

leetcode-easy-string- 38 Count and Say的更多相关文章

  1. leetCode练题——38. Count and Say

    1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...

  2. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  3. LeetCode题解38.Count and Say

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

  4. LeetCode - 38. Count and Say

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

  5. Leetcode easy

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. Leetcode 题目整理-8 Count and Say

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

  7. [LeetCode&Python] Problem 696. Count Binary Substrings

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  8. 38.Count and Say 报数

    [抄题]: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 1 ...

  9. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  10. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

随机推荐

  1. python中字符串格式化的意义(化妆)

    格式 描述%% 百分号标记 #就是输出一个%%c 字符及其ASCII码%s 字符串%d 有符号整数(十进制)%u 无符号整数(十进制)%o 无符号整数(八进制)%x 无符号整数(十六进制)%X 无符号 ...

  2. postgres日常操作

    1.启动pgsl数据库 [postgres@master ~]$ pg_ctl start [postgres@master data]$ pg_ctl -D /usr/local/pgsql/dat ...

  3. Apache(web服务器)与Tomcat(应用服务器)搭建集群

    web服务器:Apache.Nginx.IIS等 应用服务器:Tomcat.JBoss.Weblogic等 现在web服务器和应用服务器其实界限已经不是太清晰了,大部分的应用服务器也包含一些web服务 ...

  4. mac系统下Eclipse + pydev配置python Interpreter

    mac系统下Eclipse + pydev配置python Interpreter   之前都在windows下使用Eclipse + pydev 进行开发,未发现什么异常,最近对wxpy.itcha ...

  5. php 多肽实例

    多态定义:只关心一个接口或者基类,而不关心一个对象的具体类.(同一类型,不同结果) 这里两个例子: 第一个,我们发现,基类定义了标准,子类进行了自我规则的实现.这是多态的一个要求.同时,这是满足重写: ...

  6. shell脚本if判断语句报错[: too many arguments的两种原因

    shell脚本,if判断语句报错[: too many arguments 我遇到过两种情况: 1.第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断 ...

  7. ftp上传下载功能实现

    该程序分为客户端和服务端,目前已经实现以下功能: 1. 多用户同时登陆 2. 用户登陆,加密认证 3. 上传/下载文件,保证文件一致性 4. 传输过程中现实进度条 5. 不同用户家目录不同,且只能访问 ...

  8. ubuntu下安装tensorflow-gpu版本过程

    我之前已经安装了cpu-only版的tensorflow,所以现在要先把原先的tf卸载 sudo pip uninstall tensorflow sudo pip3 install tensorfl ...

  9. 清北学堂dp图论营游记day4

    依然zhx讲. 讲了概率与期望: 期望:事件结果的平均大小.记作E(x). E(x)=每种结果的大小与其概率的乘积的和. 例如,记掷一枚骰子的点数为x E(x)=1*(1/6)+2*(1/6)+3*( ...

  10. JS转为number的四种方法

    // 1.Number() var num1 = Number(true); console.log(num1); var num2 = Number(" ") console.l ...