Skip to content

Commit 4a1035f

Browse files
Merge pull request #2409 from eeee0717/master
Update 0062.不同路径,添加C#
2 parents bfc7ef9 + 9eb2bec commit 4a1035f

10 files changed

+231
-0
lines changed

problems/0062.不同路径.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,29 @@ object Solution {
536536
```
537537

538538
### c#
539+
```csharp
540+
// 二维数组
541+
public class Solution
542+
{
543+
public int UniquePaths(int m, int n)
544+
{
545+
int[,] dp = new int[m, n];
546+
for (int i = 0; i < m; i++) dp[i, 0] = 1;
547+
for (int j = 0; j < n; j++) dp[0, j] = 1;
548+
for (int i = 1; i < m; i++)
549+
{
550+
for (int j = 1; j < n; j++)
551+
{
552+
dp[i, j] = dp[i - 1, j] + dp[i, j - 1];
553+
}
554+
}
555+
return dp[m - 1, n - 1];
556+
}
557+
}
558+
```
539559

540560
```csharp
561+
// 一维数组
541562
public class Solution
542563
{
543564
public int UniquePaths(int m, int n)

problems/0063.不同路径II.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,30 @@ object Solution {
734734
}
735735
}
736736
```
737+
### C#
738+
```csharp
739+
public class Solution
740+
{
741+
public int UniquePathsWithObstacles(int[][] obstacleGrid)
742+
{
743+
int m = obstacleGrid.Length;
744+
int n = obstacleGrid[0].Length;
745+
int[,] dp = new int[m, n];
746+
if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) return 0;
747+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i, 0] = 1;
748+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0, j] = 1;
749+
for (int i = 1; i < m; i++)
750+
{
751+
for (int j = 1; j < n; j++)
752+
{
753+
if (obstacleGrid[i][j] == 1) continue;
754+
dp[i, j] = dp[i - 1, j] + dp[i, j - 1];
755+
}
756+
}
757+
return dp[m - 1, n - 1];
758+
}
759+
}
760+
```
737761
738762
<p align="center">
739763
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0096.不同的二叉搜索树.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,25 @@ object Solution {
328328
}
329329
}
330330
```
331+
### C#
332+
```csharp
333+
public class Solution
334+
{
335+
public int NumTrees(int n)
336+
{
337+
int[] dp = new int[n + 1];
338+
dp[0] = 1;
339+
for (int i = 1; i <= n; i++)
340+
{
341+
for (int j = 1; j <= i; j++)
342+
{
343+
dp[i] += dp[j - 1] * dp[i - j];
344+
}
345+
}
346+
return dp[n];
347+
}
348+
}
349+
```
331350

332351
<p align="center">
333352
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0343.整数拆分.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,25 @@ class Solution {
496496
}
497497
}
498498
```
499+
### C#
500+
```csharp
501+
public class Solution
502+
{
503+
public int IntegerBreak(int n)
504+
{
505+
int[] dp = new int[n + 1];
506+
dp[2] = 1;
507+
for (int i = 3; i <= n; i++)
508+
{
509+
for (int j = 1; j <= i / 2; j++)
510+
{
511+
dp[i] = Math.Max(dp[i],Math.Max(j*(i-j),j*dp[i-j]));
512+
}
513+
}
514+
return dp[n];
515+
}
516+
}
517+
```
499518

500519
<p align="center">
501520
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0377.组合总和Ⅳ.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ impl Solution {
312312
}
313313
}
314314
```
315+
### C#
316+
```csharp
317+
public class Solution
318+
{
319+
public int CombinationSum4(int[] nums, int target)
320+
{
321+
int[] dp = new int[target + 1];
322+
dp[0] = 1;
323+
for (int i = 0; i <= target; i++)
324+
{
325+
for (int j = 0; j < nums.Length; j++)
326+
{
327+
if (i >= nums[j] && dp[i] < int.MaxValue - dp[i - nums[j]])
328+
{
329+
dp[i] += dp[i - nums[j]];
330+
}
331+
}
332+
}
333+
return dp[target];
334+
}
335+
}
336+
```
315337

316338

317339
<p align="center">

problems/0416.分割等和子集.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,35 @@ object Solution {
726726
}
727727
}
728728
```
729+
### C#
730+
```csharp
731+
public class Solution
732+
{
733+
public bool CanPartition(int[] nums)
734+
{
735+
int sum = 0;
736+
int[] dp = new int[10001];
737+
foreach (int num in nums)
738+
{
739+
sum += num;
740+
}
741+
if (sum % 2 == 1) return false;
742+
int tartget = sum / 2;
743+
for (int i = 0; i < nums.Length; i++)
744+
{
745+
for (int j = tartget; j >= nums[i]; j--)
746+
{
747+
dp[j] = Math.Max(dp[j], dp[j - nums[i]] + nums[i]);
748+
}
749+
}
750+
if (dp[tartget] == tartget)
751+
return true;
752+
753+
return false;
729754

755+
}
756+
}
757+
```
730758
<p align="center">
731759
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
732760
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

problems/0474.一和零.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,33 @@ impl Solution {
533533
}
534534
}
535535
```
536+
### C#
537+
```csharp
538+
public class Solution
539+
{
540+
public int FindMaxForm(string[] strs, int m, int n)
541+
{
542+
int[,] dp = new int[m + 1, n + 1];
543+
foreach (string str in strs)
544+
{
545+
int zero = 0, one = 0;
546+
foreach (char c in str)
547+
{
548+
if (c == '0') zero++;
549+
else one++;
550+
}
551+
for (int i = m; i >= zero; i--)
552+
{
553+
for (int j = n; j >= one; j--)
554+
{
555+
dp[i, j] = Math.Max(dp[i, j], dp[i - zero, j - one] + 1);
556+
}
557+
}
558+
}
559+
return dp[m, n];
560+
}
561+
}
562+
```
536563

537564
<p align="center">
538565
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0494.目标和.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,33 @@ impl Solution {
585585
}
586586
}
587587
```
588+
### C#
589+
```csharp
590+
public class Solution
591+
{
592+
public int FindTargetSumWays(int[] nums, int target)
593+
{
594+
int sum = 0;
595+
foreach (int num in nums)
596+
{
597+
sum += num;
598+
}
599+
if (Math.Abs(target) > sum) return 0;
600+
if ((sum + target) % 2 == 1) return 0;
601+
int bagSize = (sum + target) / 2;
602+
int[] dp = new int[bagSize + 1];
603+
dp[0] = 1;
604+
for (int i = 0; i < nums.Length; i++)
605+
{
606+
for (int j = bagSize; j >= nums[i]; j--)
607+
{
608+
dp[j] += dp[j - nums[i]];
609+
}
610+
}
611+
return dp[bagSize];
612+
}
613+
}
614+
```
588615

589616
<p align="center">
590617
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0518.零钱兑换II.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ object Solution {
347347
}
348348
}
349349
```
350+
### C#
351+
```csharp
352+
public class Solution
353+
{
354+
public int Change(int amount, int[] coins)
355+
{
356+
int[] dp = new int[amount + 1];
357+
dp[0] = 1;
358+
for (int i = 0; i < coins.Length; i++)
359+
{
360+
for (int j = coins[i]; j <= amount; j++)
361+
{
362+
if (j >= coins[i])
363+
dp[j] += dp[j - coins[i]];
364+
}
365+
}
366+
return dp[amount];
367+
}
368+
}
369+
```
350370

351371
<p align="center">
352372
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/1049.最后一块石头的重量II.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,30 @@ impl Solution {
472472
}
473473
}
474474
```
475+
### C#
476+
```csharp
477+
public class Solution
478+
{
479+
public int LastStoneWeightII(int[] stones)
480+
{
481+
int[] dp = new int[15001];
482+
int sum = 0;
483+
foreach (int stone in stones)
484+
{
485+
sum += stone;
486+
}
487+
int target = sum / 2;
488+
for (int i = 0; i < stones.Length; i++)
489+
{
490+
for (int j = target; j >= stones[i]; j--)
491+
{
492+
dp[j] = Math.Max(dp[j], dp[j - stones[i]] + stones[i]);
493+
}
494+
}
495+
return sum - 2 * dp[target];
496+
}
497+
}
498+
```
475499

476500
<p align="center">
477501
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
 (0)