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. hive元数据库理解

    在hive2.1.1 里面一共有59张表 表1 VERSION ; version表存hive的版本信息,该表中数据只有一条,如果存在多条,会造成hive启动不起来. 表2  DBS select * ...

  2. Storm项目开发纪要

    1.POM引用storm-core和javax.servlet-api这两个组件,如果本地模式跑拓扑,要把<scope>provided</scope>去掉:如果远程发布运行, ...

  3. kubeadm安装k8s1.13

    1.环境介绍: centos 7.4.1708 关闭selinux和iptable,环境很重要! 主机 ip地址 cpu核数 内存 swap host解析 k8s-master 10.0.0.11 2 ...

  4. deep_learning_Function_numpy_newaxis参数

    np.newaxis的作用就是在这一位置增加一个一维,这一位置指的是np.newaxis所在的位置,比较抽象,需要配合例子理解. x1 = np.array([1, 2, 3, 4, 5]) # th ...

  5. osworkflow 入门基础

    OSWorkFlow入门指南目的 这篇指导资料的目的是介绍OSWorkflow的所有概念,指导你如何使用它,并且保证你逐步理解OSWorkflow的关键内容. 本指导资料假定你已经部署OSWorkfl ...

  6. PAT Basic 1012 数字分类 (20 分)

    给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1​​ = 能被 5 整除的数字中所有偶数的和: A​2​​ = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n ...

  7. [全局最小割][Stoer-Wagner 算法] 无向图最小割

    带有图片例子的 [BLOG] 复杂度是$(n ^ 3)$ HDU3691 // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragm ...

  8. windows开启ftp服务

    1.启动或关闭windows-->internet information services-->ftp服务器   选中 2.此电脑右键-->管理-->服务和应用程序--> ...

  9. Linux系统层级结构标准

    Linux Foundation有一套标准规范: FHS: Filesystem Hierarchy[‘haɪərɑːkɪ] Standard(文件系统层级标准)目前最新的标准是2.3版本:http: ...

  10. document.writeln绑定数据 --点击跳转添加样式

    document.writeln(" "); document.writeln(" "); document.writeln(" "); d ...