Skip to content

Commit

Permalink
add js solution for findTargetSumWays
Browse files Browse the repository at this point in the history
  • Loading branch information
jackeyjia committed Jul 5, 2021
1 parent 1da6ff7 commit c99a153
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions problems/0494.目标和.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,36 @@ func findTargetSumWays(nums []int, target int) int {
}
```

Javascript:
```javascript
const findTargetSumWays = (nums, target) => {

const sum = nums.reduce((a, b) => a+b);

if(target > sum) {
return 0;
}

if((target + sum) % 2) {
return 0;
}

const halfSum = (target + sum) / 2;
nums.sort((a, b) => a - b);

let dp = new Array(halfSum+1).fill(0);
dp[0] = 1;

for(let i = 0; i < nums.length; i++) {
for(let j = halfSum; j >= nums[i]; j--) {
dp[j] += dp[j - nums[i]];
}
}

return dp[halfSum];
};
```



-----------------------
Expand Down

0 comments on commit c99a153

Please sign in to comment.