leetcood学习笔记-38-报数
2024-09-04 19:49:41
---恢复内容开始---
题目描述:
第一次提交:
class Solution:
def countAndSay(self, n: int) -> str:
f = ""
for i in range(n-1):
count = 0
c = ''
for j in range(len(f)):
if j == 0 or (j - 1 > -1 and f[j]==f[j-1]):
count += 1
else:
c += str(count) + f[j - 1]
count = 1
c += str(count) + f[j]
i += 1
f = c; return f
方法二:递归
def countAndSay(self, n: int) -> str:
def count(n, str_num):
if n <= 1:
return str_num
new_str_num = ""
cnt, old = 1, str_num[0]
for ch in str_num[1:]:
if ch != old:
new_str_num += str(cnt) + old
cnt, old = 1, ch
else:
cnt += 1
if cnt:
new_str_num += str(cnt) + str_num[-1]
return count(n-1, new_str_num)
return count(n, "")
最新文章
- 关于handler 和 looper 的问题
- height:100%不起作用(无效),div全屏
- SharePoint 2013 开发——搜索架构及扩展
- xfce4桌面自动整理脚本
- Linux运维命令之一
- 关于easyui的一些小知识点(1)
- 关于font awesome或Glyphicons字体图标不能正确显示的问题
- IOS开发UIImage中stretchableImageWithLeftCapWidth方法的解释
- java单元测试(Junit)
- jquery选择器 之 获取父级元素、同级元素、子元素 - yes的日志 - 网易博客
- api接口json串换行
- SpringMVC4+MyBatis+SQL Server2014+druid 监控SQL运行情况
- 【SqlServer系列】子查询
- 【渗透课程】第三篇-体验http协议的应用
- Oracle DBA 常用查询
- python 类和元类(metaclass)的理解和简单运用
- django之路由层
- 【UML】NO.47.EBook.5.UML.1.007-【UML 大战需求分析】- 部署图(Deployment Diagram)
- (转)C# Windows服务 弹出消息提醒框
- hdu4292 Food 最大流