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个 ...
随机推荐
- 01.base-v1.js
/* Date: 2014-01-21 */ function login() { return location.href = "https://passport.jd.com/new/l ...
- C++学习32 重载new和delete运算符
内存管理运算符 new.new[].delete 和 delete[] 也可以进行重载,其重载形式既可以是类的成员函数,也可以是全局函数.一般情况下,内建的内存管理运算符就够用了,只有在需要自己管理内 ...
- Oracle中的多表查询
多表查询 l 笛卡尔积: N*M l 使用关联字段消除笛卡尔积的多余数据: SELECT EMP.*,DEPT.DNAME,DEPT.LOC FROM EMP, DEPT WHERE EMP.DEPT ...
- c语言实现词频统计
需求: 1.设计一个词频统计软件,统计给定英文文章的单词频率. 2.文章中包含的标点不计入统计. 3.将统计结果以从大到小的排序方式输出. 设计: 1.因为是跨专业0.0···并不会c++和java, ...
- [HDU 3689]Infinite monkey theorem (KMP+概率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3689 黄老师说得对,题目只有做wa了才会有收获,才会有提高. 题意:一个猴子敲键盘,键盘上有n个键,猴 ...
- asp.net 自定义文本框
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- [原]iptables的NAT策略
#*nat #:PREROUTING ACCEPT [:] #:POSTROUTING ACCEPT [:] #:OUTPUT ACCEPT [:] # #-A PREROUTING –s IP1 - ...
- SparkSQL使用之Thrift JDBC server
Thrift JDBC Server描述 Thrift JDBC Server使用的是HIVE0.12的HiveServer2实现.能够使用Spark或者hive0.12版本的beeline脚本与JD ...
- 怎样对CODESOFT中的条形码进行黑白转换
CODESOFT 2015标签设计软件能 够提供无与伦比的灵活性.功能和支持,其面对的用户也是极其的广泛.对于一些需要打印黑白反转条形码的特殊用户,例如使用黑色标签纸的用 户,CODESOFT 2 ...
- CSS3里的常用选择器总结
选择器 属性选择器: img[src="images/2.jpg"] 开头匹配: a[href ^="page/"] ...