diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index 0b53e698..4c9317bc 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -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); + } +} +``` + + + + + -----------------------