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]; + } +} +``` + + + -----------------------
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]; + } +} +``` + + + -----------------------
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); + } +} +``` + + + + + -----------------------
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]); + } +} +``` + + + -----------------------