38. Count and Say
来源:互联网 发布:php 指定变量类型 编辑:程序博客网 时间:2024/06/05 12:10
The count-and-say sequence is the sequence of integers with the first five terms as following:
1. 12. 113. 214. 12115. 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 term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
有一个小技巧是 在字符串末尾加 一个星号,方便计数
class Solution(object): def countAndSay(self, n): if n == 1: return "1" if n == 2: return "11" result = self.countAndSay(n-1) + '*' cnt = len(result) s = "" count = 1 for i in range(cnt-1): if result[i] == result[i+1]: count+=1 else: s = s + str(count) + result[i] count = 1 return s
阅读全文
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- Golang 控制并发数以及定时执行
- 块状元素和内联元素的区别
- 一头坑进Redis之五大数据类型
- PAT 1015 德才论
- C++面向对象
- 38. Count and Say
- 文章标题
- 并发程序设计——三个顺序线程练习
- POJ3734 比矩阵快速幂简单的方法
- 二进制算法
- jQuery的选择器
- matplotlib入门教程--1(条形图, 直方图, 盒须图, 饼图)
- Mapper.xml映射文件---Mybatis学习笔记(八)
- 征战kaggle.0