Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2bbf70

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 parent fec58bd commit a2bbf70

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+
class Solution {
4+
public int findTargetSumWays(int[] nums, int target) {
5+
return helper(nums, target, nums.length, 0);
6+
}
7+
8+
public int helper(int[] nums, int target, int n, int temp) {
9+
if (n == 0) {
10+
if (target == temp) return 1;
11+
else return 0;
12+
}
13+
else {
14+
return helper(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)