[leetcode]#38. Count and Say
来源:互联网 发布:java方法重载作用 编辑:程序博客网 时间:2024/06/11 03:20
题目翻译
一个”数和说(count and say)“序列是像这样的一个整数序列:1, 11, 21, 1211, 111221, …。
1 读作 “1个1” 或 11.
11 读作 “2个1” 或 21.
21 读作 “1个2,1个1” 或 1211.
给定整数n,生成第n个序列。
注意:整数序列用字符串表示。思路方法
这个问题重点首先是要读懂题目。然后在想这个过程的时候,你可能会想的比较复杂,比如,会不会出现连续十几个一样的数字,比如1111111111,读作“ten 1”,然后转换成101。实际上,有大神已经证明,这个序列里的数不会超过4。这边搬运了一下。
class Solution(object): def countAndSay(self, n): """ :type n: int :rtype: str """ res = '1' for i in xrange(n-1): new_res = '' j = 0 while j < len(res): count = 1 while j < len(res)-1 and res[j] == res[j+1]: j = j + 1 count = count + 1 j = j + 1 new_res = new_res + str(count) + res[j-1] res = new_res return res
阅读全文
0 0
- [LeetCode]38.Count and Say
- LeetCode --- 38. Count and Say
- LeetCode 38.Count and Say
- [Leetcode] 38. Count and Say
- 38. Count and Say LeetCode
- Leetcode 38. Count and Say
- leetcode 38. Count and Say
- [leetcode] 38. Count and Say
- LeetCode-38.Count and Say
- LeetCode *** 38. Count and Say
- leetcode 38. Count and Say
- LeetCode-38. Count and Say
- LeetCode 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode - 38. Count and Say
- LeetCode 38. Count and Say
- Leetcode 38. Count and Say
- LeetCode 38. Count and Say
- CentOS7源码安装node.js和webSocket
- Windows Server 2008 终端服务授权激活-离线
- qt+halcon配置方法记录
- .net core 整合日志log4net
- Mysql数据库中获取系统时间,年,月,日单个获取
- [leetcode]#38. Count and Say
- document.evaluate的详细用法--使用XPath查找某些节点对象[z]
- redis cli命令
- 谈谈Web Workers
- EyforTravcel:预计2020年人工智能将推动全球旅游业收入超470亿美元
- 服务器api性能测试(ApacheBench)
- windows下搭建基于nginx的rtmp服务器
- python dota2数据 1 API
- 码云的入门教程