mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 0416.分割等和子集,添加C#
This commit is contained in:
@ -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"/>
|
||||
|
Reference in New Issue
Block a user