leetCode练题——38. Count and Say
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的更多相关文章
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 【leetcode❤python】 38. Count and Say
#-*- coding: UTF-8 -*- class Solution(object): def countAndSay(self, n): """ ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- LeetCode练题——66. Plus one
1.题目 66. Plus One——easy Given a non-empty array of digits representing a non-negative integer, plus ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetCode练题——7. Reverse Integer
1.题目: 7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...
- LeetCode练题——88. Merge Sorted Array
1.题目 88. Merge Sorted Array——Easy Given two sorted integer arrays nums1 and nums2, merge nums2 into ...
- LeetCode练题——67. Add Binary
1.题目 67. Add Binary——easy Given two binary strings, return their sum (also a binary string). The inp ...
随机推荐
- HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out的解决方法
问题描述: Pycharm创建Django项目提示:HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed o ...
- jenkins集成robot
一.jenkins集成robot的非gui的运行命令 pybot 配置文件 用例地址 或者robot 配置文件 用例地址 二.展示robot 运行结果图表 1.在系统配置中增加Rob ...
- 在Linux系统下安装jdk并配置环境变量
本文主要介绍的是如何是Linux环境下安装JDK的,因为Linux环境下,很多时候也离不开Java的,下面笔者就和大家一起分享如何jdk1.8的过程吧. 1.安装环境 操作系统:CentOS 07 6 ...
- AcWing 826. 单链表
https://www.acwing.com/activity/content/problem/content/863/1/ #include <iostream> using names ...
- Mysql中的触发器【转】
转载:https://www.cnblogs.com/chenpi/p/5130993.html 阅读目录 什么是触发器 特点及作用 例子:创建触发器,记录表的增.删.改操作记录 弊端 什么是触发器 ...
- 题解【洛谷P2615】[NOIP2015]神奇的幻方
题目描述 幻方是一种很神奇的 \(N \times N\) 矩阵:它由数字 \(1,2,3,\cdots \cdots ,N \times N\) 构成,且每行.每列及两条对角线上的数字之和都相同. ...
- Coloring Colorfully
问题 C: Coloring Colorfully 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 N块瓦片从左到右排成一行.每个块的初始颜色由长度为N的字符串S表示. ...
- 2019牛客多校第四场A meeting 思维
meeting 题意 一个树上有若干点上有人,找出一个集合点,使得所有人都到达这个点的时间最短(无碰撞) 思路 就是找树的直径,找直径的时候记得要找有人的点 #include<bits/stdc ...
- VS“无法查找或打开PDB文件”是怎么回事?如何解决
有时候,我们使用 VS(Visual Studio)编译程序时会出现“无法查找或打开PDB文件”的提示,并且此时程序会生成失败,无法运行,如下图所示: 大家不要惊慌,出现这种提示并不是代码写错了,而是 ...
- kali 安装google输入法
脑子一热装了一礼拜的kali,在20多遍的重装后终于成功了 先码一篇如何安装google输入法 首先得更新源,用leafpad /etc/apt/sources.list打开,或vi也可以,更新源百度 ...