A description of the subject
We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0).
def Fibonacci(self, n): res = [0, 1] while len(res) <= n: res.append(res[-1]+res[-2]) return res[n]
Title Description two
A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).
# -*- coding:utf-8 -*- class Solution: def jumpFloor(self, number): a = [0, 1, 2] while len(a) <=number: a.append(a[-1] + a[-2]) return a[number]
The 3 title
A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.
Analysis:
Because the n steps, the first step there are n kinds of jump method: 1 skip, skip 2 to n stages hop
skip 1, the remaining level n-1, the remaining jumps is f (n-1 )
skip 2, the remaining n-2 stage, the remaining jumps is F (n-2)
so that f (n) = f (n -1) + f (n-2) + ... + f ( 1)
since f (n-1) = f (n-2) + f (n-3) + ... + f (1)
Because the n steps, the first step there are n kinds of jump method: 1 skip, skip 2 to n stages hop
skip 1, the remaining level n-1, the remaining jumps is f (n-1 )
skip 2, the remaining n-2 stage, the remaining jumps is F (n-2)
so that f (n) = f (n -1) + f (n-2) + ... + f ( 1)
since f (n-1) = f (n-2) + f (n-3) + ... + f (1)
Therefore, f (n) = 2 * f (n-1)
def jumpFloorII(self, number): if number <= 1: return number return 2 * self.jumpFloorII(number - 1)