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个 ...
随机推荐
- Java注解处理器--annotation学习四
Java中的注解(Annotation)是一个很神奇的东西,特别现在有很多Android库都是使用注解的方式来实现的.一直想详细了解一下其中的原理.很有幸阅读到一篇详细解释编写注解处理器的文章.本文的 ...
- linux查看和开放某端口
查看某端口是否已打开: netstat -nulp //UDP端口netstat -ntlp //TCP端口 或者:lsof -i:port-num 开放某端口: 在/etc/sysconfig/ip ...
- 30岁IT男连续工作一个月 突然失聪
连续开发软件一个月,30 岁男子突然听不见声音了.近日,浙江省中山医院针灸科主任高宏主任中医师接诊了这名患者.高主任说,现在很多年轻人工作压力大,得突发性耳聋的越来越多,这种病听着不是威胁生命的大病, ...
- ubuntu14.04 wifi驱动安装
重装linux后,一直搜不到wlan0,无法启动wifi,经过重重努力,终于成功,在此简单记录一下. 1. 查看网卡类型: ~$ lspci -nn -d 14e4: :]: Broadcom Cor ...
- Java中的GC操作及相关概念
一.GC Roots Tracing的基本思路:通过一系列名为"GC Roots"的对象作为起始点,从这些节点开始向下搜索,搜索所经过的路径称为引用链(Reference Chai ...
- 20145305 《Java程序设计》第6周学习总结
教材学习内容总结 1.输入串流代表对象为java.io.InputStream实例,输出串流代表对象为java.io.OutputStream实例 2.InputStream与OutputStream ...
- cocso2d-x改变精灵图片
cocos2d-x 改变精灵图片的2种方法. 1. // 首先载入贴图集 CCSpriteBatchNode *spriteBatch=CCSpriteBatchNode::batchNodeWith ...
- mac下配置eclipse的hadoop环境
下载eclipse-jee-mars-1-macosx-cocoa-x86_64.tar 右键显示包内容,将hadoop-eclipse-plugin-2.6.0.jar拷入到刚显示的包的plugin ...
- Antlr学习
参加工作之后,接触DSL领域语言,了解了编译原理. 比如Hibernate.Hive等的HQL都是基于antlr编写的 所以,如果想自己实现一套DSL语言,我们可以基于antlr做词法分析与语法分析 ...
- Linux安装MySQL的两种方法
转载:http://blog.csdn.net/superchanon/article/details/8546254/ 1. 运行平台:CentOS 6.3 x86_64,基本等同于RH ...