From f48f7b0aa7b9cf5e81ee3405249e0c91296418ce Mon Sep 17 00:00:00 2001 From: xiaoyu2018 <861900161@qq.com> Date: Sun, 25 Sep 2022 15:54:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00509=E6=96=90=E6=B3=A2?= =?UTF-8?q?=E9=82=A3=E5=A5=91=E6=95=B0C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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); + } +} +``` + + + + + -----------------------