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. 来了,就给自己立个flag

    2019-09-16,刚刚申请的博客园. 不知道不觉,自己已经大四毕业了. 说来惭愧,已经接触IT这方面已经四年了. 但仍然感觉自己像个萌新,啥也不懂,这也不会,那也不会. 8月开始在公司大佬的指导下 ...

  2. nginx配置访问黑名单-2

    在Nginx服务器上屏蔽IP 1.查找要屏蔽的ip awk '{print $1}' nginx.access.log |sort |uniq -c|sort -n nginx.access.log ...

  3. 【网页浏览】怀旧xp画图网页版

    非常古老的WindowsXP画图工具 传送链接

  4. APL: ANSYS Power Library

    1. creating accurate switching current waveforms (profiles) 2.output-state dependent decoupling capa ...

  5. gRPC in ASP.NET Core 3.x -- Protocol Buffer(3)更新消息类型

    当你第一次定义Protocol Buffer的消息的时候,你肯定会给消息设定一套规则需求.但是随着时间的推进,你的业务可能会发生了变化,与此同时,你的Protocol Buffer消息类型的需求也会随 ...

  6. js变量提升的坑

    关于js变量提升 变量提升 在js函数内部是可以直接修改全局的变量的,个人感觉是不好的设计, 但是确实存在这个概念 原理: 先查看有没有函数变量bb 查看形参有没有bb 查看全局有没有bb 报错, 找 ...

  7. ubuntu刪除軟件

    1.打开一个终端,输入dpkg --list ,按下Enter键,终端输出以下内容,显示的是你电脑上安装的所有软件2.在终端中找到你需要卸载的软件的名称,列表是按照首字母排序的.3.在终端上输入命令s ...

  8. MySQL执行外部sql脚本文件的命令

    sql脚本是包含一到多个sql命令的sql语句,我们可以将这些sql脚本放在一个文本文件中(我们称之为“sql脚本文件”),然后通过相关的命令执行这个sql脚本文件.基本步骤如下:1.创建包含sql命 ...

  9. 概率DP hdu 3366 .

    题意:一个人被困在一个城堡里,面前有n条路,他自己有m百万元,选择每一条路都有p概率通过,q概率遇到士兵,1-p-q概率道路不通:遇到士兵的话需要上交1百万,如果不够钱,则被杀死,问的是最优情况下多少 ...

  10. 旋转坐标+前缀和(zqu 25001)

    本题题意:在一个矩阵中,去随机一点,设定一个步数K,求出从这个点可以走到的范围的和,求最大值 思路:这个范围的和是一个菱形,我们把他旋转45°,然后成为一个正放的矩阵,求出二维前缀和 然后用前缀和的性 ...