Skip to content

Commit

Permalink
Update 0494.目标和.md
Browse files Browse the repository at this point in the history
0494.目标和新增C语言实现
  • Loading branch information
LYT0905 committed Mar 7, 2024
1 parent 64399dd commit d6a37be
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion problems/0494.目标和.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,42 @@ impl Solution {
}
}
```
```c
int getSum(int * nums, int numsSize){
int sum = 0;
for(int i = 0; i < numsSize; i++){
sum += nums[i];
}
return sum;
}

int findTargetSumWays(int* nums, int numsSize, int target) {
int sum = getSum(nums, numsSize);
int diff = sum - target;
// 两种情况不满足
if(diff < 0 || diff % 2 != 0){
return 0;
}
int bagSize = diff / 2;
int dp[numsSize + 1][bagSize + 1];
dp[0][0] = 1;
for(int i = 1; i <= numsSize; i++){
int num = nums[i - 1];
for(int j = 0; j <= bagSize; j++){
dp[i][j] = dp[i - 1][j];
if(j >= num){
dp[i][j] += dp[i - 1][j - num];
}
}
}
return dp[numsSize][bagSize];
}
```
### C#
```csharp
public class Solution
{
Expand Down Expand Up @@ -617,4 +652,3 @@ public class Solution
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>

0 comments on commit d6a37be

Please sign in to comment.