38. Count and Say
来源:互联网 发布:怎样评价日剧 知乎 编辑:程序博客网 时间:2024/06/17 14:39
题意: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.
思路:这题貌似很难找到什么规律,直接生成吧
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ ans= "1" for i in xrange(n-1): cnt, index= 1, len(ans) for j in xrange(len(ans)): if j+1<index and ans[j]==ans[j+1]: cnt+=1 else: ans+=str(cnt)+str(ans[j]) cnt = 1 ans = ans[index:] return ans
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
- 类成员方法如何调用类成员属性和方法
- HDU 2600 War 哈希
- qt designer 界面设计填坑之路 一
- ant利用正则表达式获取文件夹名
- google 选择坐标
- 38. Count and Say
- android中getWidth()和getMeasuredWidth()之间的区别
- apache安装https证书
- UEFI/Legacy的设置更改 以及硬盘模式的更改
- js中的变量提升
- 使用nginx与nginx-rtmp-module搭建流媒体服务器
- SQL优化,oracle trace使用
- web.xml中url-pattern匹配规则
- tomcat启动,但是spring配置的bean并没有初始化