Skip to content

Commit

Permalink
添加了0509.斐波那契数列Rust两种解法的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonLight-Sherry committed Mar 31, 2022
1 parent 46487f9 commit 00dc57f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion problems/0509.斐波那契数.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,30 @@ int fib(int n){
return fib(n-1) + fib(n-2);
}
```
### Rust
动态规划:
```Rust
pub fn fib(n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![0; 31];
dp[1] = 1;
for i in 2..=n {
dp[i] = dp[i - 1] + dp[i - 2];
}
dp[n]
}
```


递归实现:
```Rust
pub fn fib(n: i32) -> i32 {
//若n小于等于1,返回n
f n <= 1 {
return n;
}
//否则返回fib(n-1) + fib(n-2)
return fib(n - 1) + fib(n - 2);
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 00dc57f

Please sign in to comment.