admin 管理员组文章数量: 1087678
[勇者闯LeetCode] 70. Climbing Stairs
[勇者闯LeetCode] 70. Climbing Stairs
Description
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
Information
- Tags: Dynamic Programming
- Difficulty: Easy
Solution
实质上是Fibonacci Number:
Fib(n)=Fib(n−1)+Fib(n−2),Fib(1)=1,Fib(2)=2 ,
即爬到n阶的方法数等于爬到n-1阶的方法数和爬到n-2阶的方法数之和,爬到1阶的方法数是1,爬到2阶的方法数是2。
class Solution(object):def climbStairs(self, n):""":type n: int:rtype: int"""prev, cur = 0, 1for i in range(n):prev, cur = cur, cur + prevreturn cur
本文标签: 勇者闯LeetCode 70 Climbing Stairs
版权声明:本文标题:[勇者闯LeetCode] 70. Climbing Stairs 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1694420231a251839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论