Count and Say [LeetCode 38]
1- 问题描述
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 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, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
2- 思路分析
计算第n次输出,需顺序统计第n-1次各个字符出现的次数,然后将字符个数和字符合并。如第3次为'1211',从左到右,1个'1'、1个'2'、2个'1',所以下一个数为'111221'。本题关键即是分离出各相同字符字串。如'1211'——>'1' '2' '11'.
以下Python实现,给出2种实现分离的方法:其中第一种则使用栈从后往左解析,第二种为Kainwen给出,使用生成器(T_T,不懂)。
3- Python实现
# -*- coding: utf-8 -*-
# 方法1 栈 class Solution:
# @param {integer} n
# @return {string}
def countAndSay(self, n):
s = [] # 列表保存第n次结果
s.append('')
if n == 1: return s[0]
for i in range(1, n):
stack = [] # 栈用于存储每次pop的元素
res = [] # 每解析完一组相同字符字串,将str(长度),字符存入
last = list(s[-1]) # 上一次的结果
while True:
try:
end = last.pop() # 取出列表右端元素
except: # 若列表为空,说明已经遍历完,统计栈内元素个数,跳出循环
res.insert(0, str(len(stack)))
res.insert(1, stack[-1])
break
if not stack: # 若栈为空,则取出的元素直接入栈,进入下一次循环
stack.append(end)
continue
if stack[-1] == end: # 若栈不为空,判断取出元素是否和栈内元素相同,相同则入栈
stack.append(end)
else: # 不同,则统计栈内元素,然后清空栈,将新元素入栈
res.insert(0, str(len(stack)))
res.insert(1, stack[-1])
stack = []
stack.append(end)
s.append(''.join(res)) # 拼接长度和字符
return s[n-1]
# -*- coding: utf-8 -*-
# 方法2 生成器
from StringIO import StringIO class Solution:
# @param {integer} n
# @return {string}
def countAndSay(self, n):
s = []
s.append('')
for i in range(1, n):
a = s[-1]
b = StringIO(a)
c = ''
for i in self._tmp(b):
c += str(len(i)) + i[0]
s.append(c)
return s[n-1] def _tmp(self, stream):
tmp_buf = []
while True:
ch = stream.read(1)
if not ch:
yield tmp_buf
break
if not tmp_buf:
tmp_buf.append(ch)
elif ch == tmp_buf[-1]:
tmp_buf.append(ch)
else:
yield tmp_buf
tmp_buf = [ch,]
Count and Say [LeetCode 38]的更多相关文章
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
- LeetCode 38 Count and Say(字符串规律输出)
题目链接:https://leetcode.com/problems/count-and-say/?tab=Description 1—>11—>21—>1211—>111 ...
- LeetCode(38)题解: Count and Say
https://leetcode.com/problems/count-and-say/ 题目: The count-and-say sequence is the sequence of integ ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java [leetcode 38]Count and Say
题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...
- [leetcode]38. Count and Say数数
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- [LeetCode] 38. Count and Say_Easy
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Leetcode 38 Count and Say 传说中的递推
class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...
- leetcode 38 Count and Say ---java
这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 --> 111221 --> 312211 --> ..... 1个 ...
随机推荐
- NoSQL与关系型数据库比较
虽然09年出现了比较激进的文章<关系数据库已死>,但是我们心里都清楚,关系数据库其实还活得好好的,你还不能不用关系数据库.但是也说明了一个事实,关系数据库在处理WEB2.0数据的时候,的确 ...
- bootstrap-输入框组
<!-- input-group 只能针对输入框,输入框组 input-group-addon 给输入框前后添加的额外元素 input-group-btn 添加的额外元素是按钮: --> ...
- EF下CodeFirst、DBFirst与ModelFirst分析
EF4.1有三种方式来进行数据操作及持久化.分别是Database-First,Model-First,Code-first:1.Database First是基于已存在的数据库,利用某些工具(如Vs ...
- JavaScript【面向对象】-静态方法-私有方法-公有方法-特权方法
JavaScript面向对象是近年来比较火的一个概念了,由于小弟才疏学浅,虽然做过不少的web项目,看了网上很多深奥的资料和教程,还是对他们深奥 的理论一知半解,前段时间看了点书,总算有了自己的理解, ...
- Perl模块的安装方法
1. 下载离线安装包 *.tar.gz的形式解包后,#perl Makefile.PL#make#make install 2. 在联网的情况下,通过CPAN安装# perl -MCPAN -e sh ...
- maven项目导入eclipse
maven项目的配置文件 web java文件都在src下面 src/main/java src/main/webapp src/main/webapp/web-inf 导入后要重新添加jar包,设置 ...
- JavaScript解耦记
有两个页面A.B.页面A调用页面B.B页面还通过异步加载方式包含一个javascipt(例如叫:ClassHeaderEvaluation.js)文件.问题场景:A页面通过异步请求方式请求B,并在B的 ...
- CentOS 6 安装 Python3.5以及配置Django
http://www.jianshu.com/p/6199b5c26725 文/FiveStrong(简书作者)原文链接:http://www.jianshu.com/p/6199b5c26725著作 ...
- ArcGIS Engine 下投影坐标和经纬度坐标的相互转换
ArcGIS Engine 下投影坐标和经纬度坐标的相互转换 投影转经纬度 ); pPoint.Project(pSRF.CreateGeographicCoordinateSystem((int)e ...
- MySQL数据库优化技术概述
对于一个以数据库为中心的应用,数据库的优化直接影响到程序的性能,因此数据库性能至关重要.一般来说,要保证数据库的效率,要做好以下几个方面的工作: 1. 数据库表设计: 表的设计合理化(符合3NF): ...