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