forked from PawanJaiswal08/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTargetSum.java
More file actions
25 lines (23 loc) · 752 Bytes
/
TargetSum.java
File metadata and controls
25 lines (23 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// https://leetcode.com/problems/target-sum/
public class TargetSum {
static int count;
public static void main(String[] args) {
int[] nums = {1, 1, 1, 1, 1};
System.out.println(findTargetSumWays(nums, 3));
}
public static int findTargetSumWays(int[] nums, int target){
count = 0;
TargetSum(nums, target, 0, 0);
return count;
}
public static void TargetSum(int[] nums, int target, int index, int val){
if(index == nums.length){
if(target == val){
count++;
}
return;
}
TargetSum(nums, target, index+1, val+nums[index]);
TargetSum(nums, target, index+1, val-nums[index]);
}
}