Merge pull request #2409 from eeee0717/master

Update 0062.不同路径,添加C#
This commit is contained in:
程序员Carl
2024-01-25 11:46:46 +08:00
committed by GitHub
10 changed files with 231 additions and 0 deletions

View File

@ -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)

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">

View File

@ -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;
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -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];
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">