509. Fibonacci Number
The Fibonacci numbers, commonly denoted F(n)
form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0
and 1
. That is,
Given N
, calculate F(N)
.
Example 1:
Example 2:
Example 3:
解题要点:
创建一个新的数组串,先声明0和1位上的数分别为,0跟1。然后使用dp的方法,轮流加进数组后续数位上的数字,返回index为N的数。
方法二:
根据Fibonacci定理,当前数等于前两位数相加,先声明两个初始值0和1,用for loop循环加入到当前数,for loop结束后返回它。
Last updated
Was this helpful?