From 8618c4491541b7ed4ef945952f1646909a6005d5 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 13 Jan 2024 10:50:06 +0800 Subject: [PATCH 01/10] =?UTF-8?q?Update=200062.=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0062.不同路径.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 5131fdbb..207a66ee 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -536,8 +536,29 @@ object Solution { ``` ### c# +```csharp +// 二维数组 +public class Solution +{ + public int UniquePaths(int m, int n) + { + int[,] dp = new int[m, n]; + for (int i = 0; i < m; i++) dp[i, 0] = 1; + for (int j = 0; j < n; j++) dp[0, j] = 1; + for (int i = 1; i < m; i++) + { + for (int j = 1; j < n; j++) + { + dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; + } + } + return dp[m - 1, n - 1]; + } +} +``` ```csharp +// 一维数组 public class Solution { public int UniquePaths(int m, int n) From 08617fdff7d13cd4dcd8f512baccfdad5f64f766 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 14 Jan 2024 09:55:53 +0800 Subject: [PATCH 02/10] =?UTF-8?q?Update=200063.=E4=B8=8D=E5=90=8C=E8=B7=AF?= =?UTF-8?q?=E5=BE=842=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 8b842858..8c208ea8 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -734,6 +734,30 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int UniquePathsWithObstacles(int[][] obstacleGrid) + { + int m = obstacleGrid.Length; + int n = obstacleGrid[0].Length; + int[,] dp = new int[m, n]; + if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) return 0; + for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i, 0] = 1; + for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0, j] = 1; + for (int i = 1; i < m; i++) + { + for (int j = 1; j < n; j++) + { + if (obstacleGrid[i][j] == 1) continue; + dp[i, j] = dp[i - 1, j] + dp[i, j - 1]; + } + } + return dp[m - 1, n - 1]; + } +} +```

From 0076152aecfff72cae6c53f4c36b29faee62e47d Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 15 Jan 2024 09:52:08 +0800 Subject: [PATCH 03/10] =?UTF-8?q?Update=200343.=E6=95=B4=E6=95=B0=E6=8B=86?= =?UTF-8?q?=E5=88=86=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0343.整数拆分.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 2e17caf5..bbbd5c63 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -496,6 +496,25 @@ class Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int IntegerBreak(int n) + { + int[] dp = new int[n + 1]; + dp[2] = 1; + for (int i = 3; i <= n; i++) + { + for (int j = 1; j <= i / 2; j++) + { + dp[i] = Math.Max(dp[i],Math.Max(j*(i-j),j*dp[i-j])); + } + } + return dp[n]; + } +} +```

From 57025dcf3765174b867bc9710b57acf8a85b5a92 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Tue, 16 Jan 2024 11:11:28 +0800 Subject: [PATCH 04/10] =?UTF-8?q?Update=200096.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0096.不同的二叉搜索树.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md index d109165b..15b99083 100644 --- a/problems/0096.不同的二叉搜索树.md +++ b/problems/0096.不同的二叉搜索树.md @@ -328,6 +328,25 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int NumTrees(int n) + { + int[] dp = new int[n + 1]; + dp[0] = 1; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= i; j++) + { + dp[i] += dp[j - 1] * dp[i - j]; + } + } + return dp[n]; + } +} +```

From 064e582b1e74826e3b60b379aff9c3ee3c41d689 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 17 Jan 2024 10:37:45 +0800 Subject: [PATCH 05/10] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0416.分割等和子集.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index 81eb4c80..71e01ae3 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -726,7 +726,35 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public bool CanPartition(int[] nums) + { + int sum = 0; + int[] dp = new int[10001]; + foreach (int num in nums) + { + sum += num; + } + if (sum % 2 == 1) return false; + int tartget = sum / 2; + for (int i = 0; i < nums.Length; i++) + { + for (int j = tartget; j >= nums[i]; j--) + { + dp[j] = Math.Max(dp[j], dp[j - nums[i]] + nums[i]); + } + } + if (dp[tartget] == tartget) + return true; + return false; + + } +} +```

From 49e386d20ba95edeffba686584a6b123597ddbba Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Thu, 18 Jan 2024 10:28:55 +0800 Subject: [PATCH 06/10] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8F2?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1049.最后一块石头的重量II.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index cc661317..4c3c01a0 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -472,6 +472,30 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int LastStoneWeightII(int[] stones) + { + int[] dp = new int[15001]; + int sum = 0; + foreach (int stone in stones) + { + sum += stone; + } + int target = sum / 2; + for (int i = 0; i < stones.Length; i++) + { + for (int j = target; j >= stones[i]; j--) + { + dp[j] = Math.Max(dp[j], dp[j - stones[i]] + stones[i]); + } + } + return sum - 2 * dp[target]; + } +} +```

From e0b0078eea56b6af0e18487e428fa78ff93313ec Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Fri, 19 Jan 2024 10:02:55 +0800 Subject: [PATCH 07/10] =?UTF-8?q?Update=200494.=E7=9B=AE=E6=A0=87=E5=92=8C?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0494.目标和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index 2d38b4d0..e7a05d45 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -585,6 +585,33 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindTargetSumWays(int[] nums, int target) + { + int sum = 0; + foreach (int num in nums) + { + sum += num; + } + if (Math.Abs(target) > sum) return 0; + if ((sum + target) % 2 == 1) return 0; + int bagSize = (sum + target) / 2; + int[] dp = new int[bagSize + 1]; + dp[0] = 1; + for (int i = 0; i < nums.Length; i++) + { + for (int j = bagSize; j >= nums[i]; j--) + { + dp[j] += dp[j - nums[i]]; + } + } + return dp[bagSize]; + } +} +```

From fddab1a7f4442e7cebf70f118b6157c6dfb3d67e Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sat, 20 Jan 2024 09:51:00 +0800 Subject: [PATCH 08/10] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C=E9=9B=B6?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 8f6197ac..904d941e 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -533,6 +533,33 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int FindMaxForm(string[] strs, int m, int n) + { + int[,] dp = new int[m + 1, n + 1]; + foreach (string str in strs) + { + int zero = 0, one = 0; + foreach (char c in str) + { + if (c == '0') zero++; + else one++; + } + for (int i = m; i >= zero; i--) + { + for (int j = n; j >= one; j--) + { + dp[i, j] = Math.Max(dp[i, j], dp[i - zero, j - one] + 1); + } + } + } + return dp[m, n]; + } +} +```

From bcfa9202eba667e24df0512b842fa13669f36924 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Sun, 21 Jan 2024 10:29:39 +0800 Subject: [PATCH 09/10] =?UTF-8?q?Update=200518.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A22=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0518.零钱兑换II.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 5c6efdcb..9fbe8ba3 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -347,6 +347,26 @@ object Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int Change(int amount, int[] coins) + { + int[] dp = new int[amount + 1]; + dp[0] = 1; + for (int i = 0; i < coins.Length; i++) + { + for (int j = coins[i]; j <= amount; j++) + { + if (j >= coins[i]) + dp[j] += dp[j - coins[i]]; + } + } + return dp[amount]; + } +} +```

From 9eb2bec1b01e0baa9254401c732a6fb86d55df39 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Mon, 22 Jan 2024 10:32:00 +0800 Subject: [PATCH 10/10] =?UTF-8?q?Update=200377.=E7=BB=84=E5=90=88=E7=BB=BC?= =?UTF-8?q?=E5=90=884=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0377.组合总和Ⅳ.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index 05f852b1..a840ec9b 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -312,6 +312,28 @@ impl Solution { } } ``` +### C# +```csharp +public class Solution +{ + public int CombinationSum4(int[] nums, int target) + { + int[] dp = new int[target + 1]; + dp[0] = 1; + for (int i = 0; i <= target; i++) + { + for (int j = 0; j < nums.Length; j++) + { + if (i >= nums[j] && dp[i] < int.MaxValue - dp[i - nums[j]]) + { + dp[i] += dp[i - nums[j]]; + } + } + } + return dp[target]; + } +} +```