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 1/4] =?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); + } +} +``` + + + + + -----------------------
From 5ac7f6438110a91529b22fc45fb2a52d732cd076 Mon Sep 17 00:00:00 2001 From: xiaoyu2018 <861900161@qq.com> Date: Sun, 25 Sep 2022 16:02:41 +0800 Subject: [PATCH 2/4] =?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/0070.爬楼梯.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 0a6acf7f..903e8d58 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -425,5 +425,25 @@ object Solution { } ``` +### C# + +```c# +public class Solution { + public int ClimbStairs(int n) { + if(n<=2) return n; + int[] dp = new int[2] { 1, 2 }; + for (int i = 3; i <= n; i++) + { + int temp = dp[0] + dp[1]; + dp[0] = dp[1]; + dp[1] = temp; + } + return dp[1]; + } +} +``` + + + -----------------------
From 44c365a7baea23877053bb7966bcaaf6c81c1de8 Mon Sep 17 00:00:00 2001 From: xiaoyu2018 <861900161@qq.com> Date: Sun, 25 Sep 2022 16:13:05 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00746=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC=E6=A2=AF?= =?UTF-8?q?C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index 9d3bd7fa..4a4c0b65 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -370,5 +370,26 @@ object Solution { } ``` +### C# + +```c# +public class Solution +{ + public int MinCostClimbingStairs(int[] cost) + { + int[] dp=new int[2] { cost[0], cost[1] }; + for (int i = 2; i < cost.Length; i++) + { + int temp = Math.Min(dp[0], dp[1])+cost[i]; + dp[0]=dp[1]; + dp[1]=temp; + } + return Math.Min(dp[0],dp[1]); + } +} +``` + + + -----------------------
From 4e9ff712f71005b45e6ac0d7fbc2b0f0b779710e Mon Sep 17 00:00:00 2001 From: xiaoyu2018 <861900161@qq.com> Date: Sun, 25 Sep 2022 16:23:12 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00062=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0062.不同路径.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 5790df69..79e49b87 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -452,5 +452,25 @@ object Solution { } ``` +### c# + +```c# +public class Solution +{ + public int UniquePaths(int m, int n) + { + int[] dp = new int[n]; + for (int i = 0; i < n; i++) + dp[i] = 1; + for (int i = 1; i < m; i++) + for (int j = 1; j < n; j++) + dp[j] += dp[j - 1]; + return dp[n - 1]; + } +} +``` + + + -----------------------