mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加0509斐波那契数C#版本
This commit is contained in:
@ -370,5 +370,45 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C#
|
||||||
|
|
||||||
|
动态规划:
|
||||||
|
|
||||||
|
```c#
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int Fib(int n)
|
||||||
|
{
|
||||||
|
if(n<2) return n;
|
||||||
|
int[] dp = new int[2] { 0, 1 };
|
||||||
|
for (int i = 2; i <= n; i++)
|
||||||
|
{
|
||||||
|
int temp = dp[0] + dp[1];
|
||||||
|
dp[0] = dp[1];
|
||||||
|
dp[1] = temp;
|
||||||
|
}
|
||||||
|
return dp[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
递归:
|
||||||
|
|
||||||
|
```c#
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int Fib(int n)
|
||||||
|
{
|
||||||
|
if(n<2)
|
||||||
|
return n;
|
||||||
|
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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user