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

Commita2bbf70

Browse files
Create 494-Target-Sum.java
Just the brute-force solution. If anyone has solved it the efficient way then please add that also.
1 parentfec58bd commita2bbf70

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎java/494-Target-Sum.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Brute-force solution (accepted)
2+
3+
classSolution {
4+
publicintfindTargetSumWays(int[]nums,inttarget) {
5+
returnhelper(nums,target,nums.length,0);
6+
}
7+
8+
publicinthelper(int[]nums,inttarget,intn,inttemp) {
9+
if (n ==0) {
10+
if (target ==temp)return1;
11+
elsereturn0;
12+
}
13+
else {
14+
returnhelper(nums,target-nums[n-1],n-1,temp) +helper(nums,target+nums[n-1],n-1,temp);
15+
}
16+
}
17+
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp