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. Maven之私服配置

    一.配置从私服下载 从私服下载主要是将 central 库的下载地址从https://repo1.maven.org/maven2/修改为私服地址,比如http://localhost:8081/re ...

  2. vue--支付宝支付

    1.支付宝支付:前端发起一个请求,后台返回一个页面,直接将返回的页面(一个表单),再执行表单提交 okFryOtherPayHandler(){ let reqBody = {}; reqBody.o ...

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

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

  4. 如何删掉git版本库master分支的第一个commit

    这个操作会将库清空,一般来说在建库开始的时候操作. 适用场景: git init初始化版本库之后,提交第一个点之后发现这个点出问题了,但是此时内心如果有洁癖的话, 你会觉得不完美,很想把这个点干掉重来 ...

  5. mybaites 入门到精通

    这个mybatis教程也不错:http://limingnihao.iteye.com/blog/781671 MyBatis 目录(?)[-] mybatis实战教程mybatis in actio ...

  6. [uboot] (番外篇)global_data介绍(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ...

  7. CentOS下phpMyAdmin安装

    1.phpMyAdmin官网下载https://www.phpmyadmin.net/downloads/ 2.下载程序包 wget https://files.phpmyadmin.net/phpM ...

  8. 【每日一包0003】kind-of

    github地址:https://github.com/ABCDdouyae... kind-of 判断数据类型用法:kind-of(date)返回:string 数据类型 January undef ...

  9. 提升Java开发效率必看!教你如何在MyEclipse中使用内联搜索

    MyEclipse CI 2019.4.0安装包下载 Inline Search取代传统Eclipse查找和替换对话框,提供更搞笑.更具侵略性的搜索体验——一种不会中断开发工作流程的工具. DevSt ...

  10. LOJ-6285-数列分块入门9

    链接: https://loj.ac/problem/6285 题意: 给出一个长为 的数列,以及 个操作,操作涉及询问区间的最小众数. 思路: vector维护每个值的出现位置, 预处理第i快到第j ...