菲波那契相关

菲波那契第n项

import sys
def Fibonacci(N):
    if N < 2:
        return 1
    a, b = 1, 1
    while N >= 2:
        a, b = b, a + b
        N -= 1
    return b

N=int(raw_input())
print(Fibonacci(N))

跳台阶

class Solution:
    def jumpFloor(self, n):
        res = [1, 1, 2]
        while len(res) <= n:
            res.append(res[-1] + res[-2])
        return res[n]

变态跳台阶

coding=utf-8
#因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
#  跳1级,剩下n-1级,则剩下跳法是f(n-1)
# 跳2级,剩下n-2级,则剩下跳法是f(n-2)
# 所以f(n)=f(n-1)+f(n-2)+...+f(1)
# 因为f(n-1)=f(n-2)+f(n-3)+...+f(1)
# 所以f(n)=2*f(n-1)
class Solution:
    def jumpFloorII(self, n):
        # write code here
        if number <= 0:
            return 0
        else:
            return pow(2,n-1)

 

posted @ 2019-05-08 16:17  sarah_wen  阅读(84)  评论(0编辑  收藏  举报