爬梯子问题与斐波那契数列
2017-09-30 11:49
218 查看
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?
一个长度为n的梯子,一次只能走1,2步,多少种走法能到顶部?
思路如下,n-1到n有2种方法,n-1走一步,或者n-2走两步
f(n)=f(n-1)+f(n-2),著名的斐波那契数列
编程如下
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=1
b=1
for i in range(n):
a,b=b,a+b
return a
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
一个长度为n的梯子,一次只能走1,2步,多少种走法能到顶部?
思路如下,n-1到n有2种方法,n-1走一步,或者n-2走两步
f(n)=f(n-1)+f(n-2),著名的斐波那契数列
编程如下
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
a=1
b=1
for i in range(n):
a,b=b,a+b
return a
相关文章推荐
- 斐波那契数列算法优化问题
- leetcode 70. Climbing Stairs爬梯子(DP问题)
- java编程:斐波那契数列经典案例:兔子问题
- Java 兔子问题(斐波那契数列)扩展篇
- 斐波那契数列及青蛙跳台阶问题
- hihoCoder #1143 : 骨牌覆盖问题·一 (斐波那契数列)
- 关于爬楼梯问题的斐波那契数列
- 【问题解决】斐波那契数列
- 递归解决-兔子问题-斐波那契数列
- 斐波那契数列【黄金分割数列】-兔子问题
- 动态规划-------一个简单爬梯子问题
- 斐波那契数列及相关问题
- “斐波那契数列”问题的递推算法
- leetcode笔记:Climbing Stairs(斐波那契数列问题)
- 7、斐波那契数列(青蛙跳问题)
- 斐波那契数列--生兔子问题
- 动态规划问题学习路线:斐波那契数列,最大递增子序列,松鼠捡苹果,最大公共子序列,字符串编辑距离
- 数学问题(1):杨辉三角、水仙花数、打印斐波那契数列
- java编程经典案例之基于斐波那契数列解决兔子问题实例
- JAVA动态规划,上梯子问题