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个 ...
随机推荐
- 解决mysql"Access denied for user'root'@'IP地址'"问题
在另一台服务器使用 MySQL-Front链接时: 解决方法: 在MySQL服务器上使用root登录后,执行如下SQL语句: mysql 登录命令: >mysql -u root -p; 然后执 ...
- 91、sendToTarget与sendMessage
Message msg = handler.obtainMessage(); msg.arg1 = i; msg.sendToTarget(); ...
- js 节流函数 throttle
/* * 频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次 * @param fn {function} 需要调用的函数 * @param delay {number} 延迟时间, ...
- Python 描述符(descriptor) 杂记
转自:https://blog.tonyseek.com/post/notes-about-python-descriptor/ Python 引入的“描述符”(descriptor)语法特性真的很黄 ...
- [C#常用代码]如何把指定文件夹中的文件移动到指定的文件夹
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 蓝桥杯---剪格子(DFS&BFS)(小总结)
问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+ ...
- Android--使用Notification在通知栏显示消息
在一个Activity中点击按钮,产生一个通知栏消息通知. package cn.luxh.mynotice; import android.os.Bundle; import android.uti ...
- LayoutInflater的实例化
获得 LayoutInflater 实例的三种方式 1. LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutI ...
- nfs挂在内核出错 T T *** ERROR: Cannot umount
今天在U-boot挂载nfs内核是出现如下错误,网上查了解决方案. SOCFPGA_CYCLONE5 # nfs 20000 192.168.0.75:/work/nfs_root/uImageWai ...
- IOS开发-跨域访问DWR方法
用Phonegap做手机客户端,服务器用spring+DWR,需要在手机端访问服务器的方法,需要做以下配置,可以参见http://www.iteye.com/topic/337460: 服务器DWR配 ...