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. vue中监听数据变化 watch

    今天做项目的时候,子组件中数据(原本固定的数据)需要父组件动态传入,如果一开始初始化用到的数据.但当时还没有获取到,初始化结束就不会更新数据了.只有监听这两个属性,再重新执行初始化. 1.watch是 ...

  2. MySQL--高性能MySQL笔记二

    人们通常使用varchar(15):来存储IP地址,然而它们其实是32位无符号整数,不是字符串,所以应该使用无符号整数存储IP地址,MySQL 提供 INET_ATON() 和 INET_NTOA() ...

  3. Linux版本显示和区别32位还是64位系统

    查看已经安装的Linux版本信息 1.cat /etc/issue 查看版本 [root@master master]# cat /etc/issue \S Kernel \r on an \m 2. ...

  4. dedecms 列表标签 去斜杠 去两边空格

    首先:将 include/arc.listview.class.php 文件的第53行: $this->Fields['title'] = ereg_replace("[<> ...

  5. Delphi Timer组件

  6. Centos7 下Yum安装OpenLdap

    网上的教程一大堆,也没用具体说明版本,所以很多操作方法都不一样,把我踩过的坑记录下来 环境: Centos7 OpenLdap 2.4.44 openldap新版本和老版本的配置方法差别特别大 安装步 ...

  7. C#实现下载Demo

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  8. python 单元测试_unittest(七)

    一.unittest中各阶段的核心概念:TestCase, TestSuite, 断言函数, TextTestRunner,TestFixture TestCase:所用用例的基类,软件测试中基本的测 ...

  9. hdu4507 吉哥系列故事——恨7不成妻[数位DP]

    这题面什么垃圾玩意儿 首先看到问题格式想到数位DP,但是求的是平方和.尝试用数位DP推出. 先尝试拼出和.设$f[len][sum][mod]$表示填到$len$位,已填位置数位和$sum$,数字取余 ...

  10. k8sConfigMap资源

    ConfigMap对象用于为容器中的应用提供配置数据以定制程序的行为,不过敏感的配置信息,例如密钥.证书等通常由Secret对象来进行配置.他们将相应的配置信息保存于对象中,而后在pod资源上以存储卷 ...