当前位置:网站首页>面试题 08.01. 三步问题
面试题 08.01. 三步问题
2022-06-09 12:25:00 【子言慕雨】
题
三步问题。有个小孩正在上楼梯,楼梯有n阶台阶,小孩一次可以上1阶、2阶或3阶。实现一种方法,计算小孩有多少种上楼梯的方式。结果可能很大,你需要对结果模1000000007。
示例1:
输入:n = 3
输出:4
说明: 有四种走法
示例2:
输入:n = 5
输出:13
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/three-steps-problem-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
ans[n]=上3步之和
代码
class Solution:
def waysToStep(self, n: int) -> int:
# 1 2 3
MOD_DATA = 1000000007
ans = [1, 2, 4]
# 注意我定义0为1 n-1 是n
for i in range(3, n):
index = i % 3
ans[index] = (ans[index - 1] + ans[index - 2] + ans[index - 3]) % MOD_DATA
return ans[(n-1)%3]
边栏推荐
- 【C语言练习——打印整数二进制的奇数位和偶数位】
- Yunna | how to manage the physical assets of the company
- 数字化转型:如何获得组织的认可?
- . Use of net mvc5 rich text [wangeditor5]
- 云呐|数据库监控工具,数据库监控运维工具
- Seven misconceptions of digital transformation
- NVIDIA releases the latest version of Tao toolkit to further simplify and accelerate AI model creation
- Database day-6
- Database day-1
- [leetcode weekly race record] record of the 294th weekly race
猜你喜欢

使用nodejs导出md/Markdown文档当中的图片到本地并替换原始图片链接为本地图片链接

互联网拓扑是怎样构成的?又代表了什么?

网络空间测绘国内外发展及现状

Network attacks focus on people's livelihood. How can we effectively defend against DDoS and apt attacks?

云呐|固定资产如何管理比较好?公司固定资产怎么管理?

JVM运行时内存区系列之方法区

Introduction to culture CNI topic 3: simple source code analysis of multi network CNI plug-ins

IDEA将现在新增加的修改合并cherry pick到之前的版本

Yuna | the importance of server monitoring and the role of monitoring management server

2021年10月4日Facebook史上最严重宕机复盘分析
随机推荐
wsl2环境搭建
What are the types and aspects of Yunna asset management system
[description of promtail configuration file]
Jstat details
C语言栈--链栈
[leetcode weekly race record] record of the 79th biweekly race + the 295th weekly race
[noip2015 improvement group] transportation plan
Yuna server monitoring visualization tool
【SignalR全套系列】之在.Net Core 中实现SignalR实时通信
Basic knowledge of modal analysis that engineers should know
软件测试工程师手把手教你如何制定测试计划
Database installation --mysql
炒作剽窃、内鬼欺诈 OpenSea上常见的NFT骗局及安全建议
[NOIP2015 提高组] 运输计划
【C语言练习——打印菱形及其变形】
Compress uploaded pictures with JS
Record the troubleshooting of high program memory consumption
Yunna | how to manage fixed assets better? How to manage the company's fixed assets?
C language -- linear list -- double linked list
云呐|数据库监控一般监控什么