/*****************************************************问题描述*************************************************
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,
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), for N > 1.
Given N, calculate F(N).
求斐波拉契数
/*****************************************************我的解答*************************************************
/**
* @param {number} N
* @return {number}
*/
var fib = function(N) {
if(N === 1 || N === 2)
{
return 1;
}
var a = 1,b = 1;
var retNum = 0;
for(var index = 3; index <= N; index++)
{
retNum = a + b;
a = b;
b = retNum;
}
return retNum;
};
leetCode刷题记录67_509_Fibonacci Number
最新推荐文章于 2024-04-24 13:04:08 发布