Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc99a153

Browse files
authored
add js solution for findTargetSumWays
1 parent1da6ff7 commitc99a153

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎problems/0494.目标和.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,36 @@ func findTargetSumWays(nums []int, target int) int {
306306
}
307307
```
308308

309+
Javascript:
310+
```javascript
311+
constfindTargetSumWays= (nums,target)=> {
312+
313+
constsum=nums.reduce((a,b)=> a+b);
314+
315+
if(target> sum) {
316+
return0;
317+
}
318+
319+
if((target+ sum)%2) {
320+
return0;
321+
}
322+
323+
consthalfSum= (target+ sum)/2;
324+
nums.sort((a,b)=> a- b);
325+
326+
let dp=newArray(halfSum+1).fill(0);
327+
dp[0]=1;
328+
329+
for(let i=0; i<nums.length; i++) {
330+
for(let j= halfSum; j>= nums[i]; j--) {
331+
dp[j]+= dp[j- nums[i]];
332+
}
333+
}
334+
335+
return dp[halfSum];
336+
};
337+
```
338+
309339

310340

311341
-----------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp