mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0509.斐波那契数.md
增加java非压缩状态版本,易于理解
This commit is contained in:
@ -186,6 +186,26 @@ class Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
//非压缩状态的版本
|
||||||
|
class Solution {
|
||||||
|
public int fib(int n) {
|
||||||
|
if (n <= 1) return n;
|
||||||
|
|
||||||
|
int[] dp = new int[n + 1];
|
||||||
|
|
||||||
|
dp[0] = 0;
|
||||||
|
dp[1] = 1;
|
||||||
|
|
||||||
|
for (int index = 2; index <= n; index++){
|
||||||
|
dp[index] = dp[index - 1] + dp[index - 2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
Reference in New Issue
Block a user