剑指 Offer 10- I:斐波那契数列

This commit is contained in:
huangge1199@hotmail.com 2021-09-04 21:52:39 +08:00
parent ff0e0a06c9
commit cb38b579ef
3 changed files with 100 additions and 1 deletions

View File

@ -0,0 +1,65 @@
//写一个函数输入 n 求斐波那契Fibonacci数列的第 n F(N)斐波那契数列的定义如下
//
//
//F(0) = 0, F(1) = 1
//F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
//
// 斐波那契数列由 0 1 开始之后的斐波那契数就是由之前的两数相加而得出
//
// 答案需要取模 1e9+71000000007如计算初始结果为1000000008请返回 1
//
//
//
// 示例 1
//
//
//输入n = 2
//输出1
//
//
// 示例 2
//
//
//输入n = 5
//输出5
//
//
//
//
// 提示
//
//
// 0 <= n <= 100
//
// Related Topics 记忆化搜索 数学 动态规划 👍 223 👎 0
package leetcode.editor.cn;
//剑指 Offer 10- I:斐波那契数列
class FeiBoNaQiShuLieLcof {
public static void main(String[] args) {
//测试代码
Solution solution = new FeiBoNaQiShuLieLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int fib(int n) {
if (n < 2) {
return n;
}
int f0 = 0;
int f1 = 1;
long result = 0;
for (int i = 2; i <= n; ++i) {
result = (f0 + f1) % 1000000007;
f0 = f1;
f1 = (int) result;
}
return (int) result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,34 @@
<p>写一个函数,输入 <code>n</code> 求斐波那契Fibonacci数列的第 <code>n</code> 项(即 <code>F(N)</code>)。斐波那契数列的定义如下:</p>
<pre>
F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.</pre>
<p>斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。</p>
<p>答案需要取模 1e9+71000000007如计算初始结果为1000000008请返回 1。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 2
<strong>输出:</strong>1
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 5
<strong>输出:</strong>5
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 <= n <= 100</code></li>
</ul>
<div><div>Related Topics</div><div><li>记忆化搜索</li><li>数学</li><li>动态规划</li></div></div><br><div><li>👍 223</li><li>👎 0</li></div>