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. JS高级---函数作为返回值使用

    函数作为返回值使用 function f1() { console.log("f1函数开始"); return function () { console.log("函数 ...

  2. STM32F103之ADC学习记录

    1.问题 1)10位ADC的误差是多少? 首先要分清分辨率与精度的区别. 10cm的尺子,有100个等分刻度,则该尺子的分辨率为1mm. 但不能说这把尺子的精度是1mm. 在冬天,尺子会热胀冷缩,依然 ...

  3. Adobe 系列下载链接

    (注意!:在下方链接前加上 "pan.baidu.com/s/" 才是正确网址,用"百度网盘"下载) Photoshop 专区(图像处理软件) Adobe Ph ...

  4. set,get,setter

    JS对象属性中get/set与getter/setter是什么 2019-01-18 15:07:44 CHENKAI188 阅读数 686更多 分类专栏: JS修仙系列   版权声明:本文为博主原创 ...

  5. swagger2 常用注解的使用

    一.@Api 效果: @Api注解放在类上面,这里的value是没用的,tags表示该controller的介绍. 二 .@ApiOperation 效果: @ApiOperation注解用于放在方法 ...

  6. c++对象的内存模式

    #include <iostream> using namespace std; class Obj { private: int* a; public: int* ga() { retu ...

  7. C#中如何将字符串或者字符串数组写入文本文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 7_1 除法(UVa725)<选择合适的枚举对象>

    如果把数字0到9分配成2个整数(各五位数),现在请你写一支程序找出所有的配对使得第一个数可以整除第二个数,而且商为N(2<=N<=79),也就是:abcde / fghijk = N这里每 ...

  9. spring boot配置druid数据连接池

    Druid是阿里巴巴开源项目中一个数据库连接池. Druid是一个jdbc组合,包含三个部分, 1.DruidDriver代理Driver,能够提供基于Filter-Chain模式得插件体系2.Dru ...

  10. 【SSH】Spring 整合 Struts

    添加 spring-struts-3.2.9.RELEASE.jar struts-config.xml 添加 <controller> <set-property property ...