1、题目

38. Count and Say

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 where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.

Note: Each term of the sequence of integers will be represented as a string.

题目解释:原题的意思就是用一个新的字符串描述上一个字符串,用数字表示上一个:
当n=1时:输出1;
当n=2时,解释1,1读作1个 ,表示为11;
当n=3时,解释上一个11,读作2个1,表示为21;(注意相同数字的描述)
当n=4时,解释上一个21,读作1个2,一个1,表示为1211;
当n=5时,解释上一个1211,读作1个1,1个2,2个1,表示为111221;
当n=6时,解释上一个111221,读作3个1,2个2,1个1,表示为312211;

2、我的解答

照搬大神的解法。。。。

 # -*- coding: utf-8 -*-
# @Time : 2020/2/5 10:54
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 38. Count and Say.py class Solution:
def countAndSay(self, n: int) -> str:
if n == 1:
return ""
elif n == 2:
return ""
string = "" #在11的基础上开始变换
for i in range(2, n):
newsb = [] #设立一个空列表
prev = string[0] #记住第一个字符
cnt = 1
for symbol in string[1:]:
if symbol == prev:
cnt += 1 # 通过循环,记住第一个字符重复的次数
else:
newsb.append(str(cnt) + prev) #若后续字符与第一个字符不重复,返回 1个”第一个字符“
prev = symbol # 改为记住后续字符
cnt = 1 # 默认该字符出现次数为1
newsb.append(str(cnt) + prev) #若后续字符与第一个字符重复,返回 几个”第一个字符“
string = "".join(newsb)
return string
print(Solution().countAndSay(4))

leetCode练题——38. Count and Say的更多相关文章

  1. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

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

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

  3. LeetCode练题——70. Climbing Stairs

    1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...

  4. LeetCode练题——66. Plus one

    1.题目 66. Plus One——easy Given a non-empty array of digits representing a non-negative integer, plus ...

  5. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  6. leetCode练题——12. Integer to Roman

    1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C,  ...

  7. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  8. LeetCode练题——88. Merge Sorted Array

    1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into  ...

  9. LeetCode练题——67. Add Binary

    1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...

随机推荐

  1. Pyarm的Pyqt的配置

    相关连接: Python PyQt 安装python3.4 x64到c盘根目录. 安装PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x64.exe 安装pycharm-professio ...

  2. c# 泛型<T>类型参数T的约束where

    在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制.如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误.这些限制称为约束.约束是使用 where 上 ...

  3. python接口自动化之用HTMLTestRunner生成html测试报告

    [第一步]:引入HTMLTestRunner包 1.下载HTMLTestRunner,下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html ...

  4. ubuntu18.04双卡机安装ubidia驱动遇到的坑

    在ubuntu 18的软件更新中选择英伟达驱动进行安装,输入nvidia-smi总是提示缺少驱动,未驱动之类的报错. 何解? 最初以为安装系统的问题,重装ubuntu也未果. 最后查了很多资料,找到一 ...

  5. js获取后台map格式数据

    后台: @RequestMapping("/dictList") @ResponseBody public Map<String, Object> positionLi ...

  6. python修改文件后缀名

    修改文件后缀名 # -*- coding: utf-8 -*- import os # # 列出当前目录下所有的文件 # filedir = 'C:\\Users\\WT\\Desktop\\test ...

  7. 在一个formitem中多input的验证方法-antd的验证

    实现效果如下: 当点击按钮的时候 对一个FormItem里的多个input/或者是input和select进行校验  同时通过Rol/Col实现布局 Rselect/input组件封装的组件如下fie ...

  8. java_爬虫_获取经过js渲染后的网页源码

    md 弄了一天了……(这个月不会在摸爬虫了,浪费生命) 进入正题: 起初是想写一个爬虫来爬一个网站的视频,但是怎么爬取都爬取不到,分析了下源代码之后,发现源代码中并没有视频的dom 但是在浏览器检查元 ...

  9. Windows下PHP安装 Imagick 扩展

    1.下载拓展下载地址一: http://windows.php.net/downloads/pecl/releases/imagick/下载地址二: https://pecl.php.net/pack ...

  10. AJAX-状态属性

    XMLHttpRequest对象的readyState属性 作用:表示xhr对象的请求状态 值:由0到4表示5个状态 0:请求尚未初始化 1:已经打开到WEB服务器的连接,正在向服务器发送请求 2:请 ...