LeetCode--038--报数(*)
问题描述:
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
1 被读作 "one 1" ("一个一") , 即 11。
11 被读作 "two 1s" ("两个一"), 即 21。
21 被读作 "one 2", "one 1" ("一个二" , "一个一") , 即 1211。
给定一个正整数 n ,输出报数序列的第 n 项。
注意:整数顺序将表示为一个字符串。
方法1:
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
ans = ""
n -= 1
while n > 0:
res = ""
pre = ans[0]
count = 1
for i in range(1, len(ans)):
if pre == ans[i]:
count += 1
else:
res += str(count) + pre
pre = ans[i]
count = 1
res += str(count) + pre
ans = res
n -= 1
return ans
变体:
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
ans = ''
while n > 1:
ans = self.countStr(ans)
n -= 1
return ans def countStr(self,s):
count = 0;ans = "";tmp = s[0]
for i in range(len(s)):
if s[i] == tmp:
count += 1
else:
ans += str(count) + tmp
tmp = s[i];count = 1
ans += str(count) + tmp
return ans
方法2:递归
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n == 1:
return ''
s = self.countAndSay(n-1)
res = ''
count = 0
for i in range(len(s)):
count += 1
if i == len(s) - 1 or s[i] != s[i+1]:
res += str(count)
res += s[i]
count = 0
return res
2018-07-23 21:30:30
LeetCode--038--报数(*)的更多相关文章
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- Leetcode 38.报数 By Python
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...
- LeetCode 38.报数(Python3)
题目: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1& ...
- [leetcode] 38. 报数(Java)(字符串处理)
38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...
- Java for LeetCode 038 Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- LeetCode 038 Count and Say
题目要求:Count and Say The count-and-say sequence is the sequence of integers beginning as follows:1, 11 ...
- C#刷遍Leetcode面试题系列连载(2): No.38 - 报数
目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...
- 多态以及 LeetCode 每日一题
1 多态 1.1 多态性 Java 引用变量有两个类型:一个是编译时类型,一个是运行时类型.前者是代码中声明这个变量时的类型,后者是由实际对象的类型决定的.当编译类型和运行类型不一样时,产生多态. c ...
- 【LeetCode】Count and Say(报数)
这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...
- LeetCode~报数(简单)
报数(简单) 题目描述: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ( ...
随机推荐
- Java Callable接口——有返回值的线程
实际开发过程中,我们常常需要等待一批线程都返回结果后,才能继续执行.<线程等待——CountDownLatch使用>中我们介绍了CountDownLatch的使用,通过使用CountDow ...
- nginx 安装步骤
1. 下载nginx,网址:http://nginx.org/download/ 2. 解压 tar -xzvf nginx-1.6.2.tar.gz 3. 建立映像文件:mount -t iso96 ...
- ajax 请求发出了,数据更改了,但是没进入success 函数 把success 换成 complete
$(function(){ $(document).on('tap','.w-location-group .mui-table-view-cell',function(){ var bool = $ ...
- Linux基础命令---diffstat
diffstat 这个程序读取diff的输出,并显示每个文件的插入.删除和修改的直方图.Diffstat是一个用于检查大型复杂修补程序文件的程序.它从包含diff输出的一个或多个输入文件中读取,生成针 ...
- web前端----JavaScript的DOM(三)
一.JS中for循环遍历测试 for循环遍历有两种 第一种:是有条件的那种,例如 for(var i = 0;i<ele.length;i++){} 第二种:for (var i in l ...
- web前端----JavaScript的DOM(一)
一.什么是HTML DOM HTML Document Object Model(文档对象模型) HTML DOM 定义了访问和操作HTML文档的标准方法 HTML DOM 把 HTML 文档呈现 ...
- C/C++笔记 #035# Makefile
相关资料: Understanding roles of CMake, make and GCC GCC and Make ( A simple tutorial, teaches u how to ...
- 更改 Centos 6 的 yum 源
1.查看当前使用的源: yum repolist all 阿里源网址,使用方法点右边的帮助可以看到:https://opsx.alibaba.com/mirror 2.更改源: 第一步:备份你的原镜像 ...
- 20145333茹翔《网络对抗》Exp9 Web安全基础实践
20145333茹翔<网络对抗>Exp9 Web安全基础实践 基础问题回答 1.SQL注入原理,如何防御 SQL注入 就是通过把SQL命令插入到"Web表单递交"或&q ...
- C# 将 Stream 写入文件
public void StreamToFile(Stream stream,string fileName) { // 把 Stream 转换成 byte[] byte[] bytes = new ...