-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTargetSum.java
36 lines (34 loc) · 1.12 KB
/
TargetSum.java
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
26
27
28
29
30
31
32
33
34
35
36
package io.ziheng.recursion.dynamicprogramming.leetcode;
import java.util.Map;
import java.util.HashMap;
/**
* LeetCode 494. Target Sum
* https://leetcode.com/problems/target-sum/
*/
public class TargetSum {
private Map<String, Integer> memory = new HashMap<>();
public int findTargetSumWays(int[] nums, int S) {
if (nums == null || nums.length == 0) {
return 0;
}
return findTargetSumWays0(0, nums.length, nums, S, 0);
}
private int findTargetSumWays0(int index, int length, int[] nums, int target, int sum) {
if (index >= length) {
if (sum == target) {
return 1;
} else {
return 0;
}
}
String pattern = index + "->" + sum;
if (memory.containsKey(pattern)) {
return memory.get(pattern);
}
int add = findTargetSumWays0(index + 1, length, nums, target, sum + nums[index]);
int minus = findTargetSumWays0(index + 1, length, nums, target, sum - nums[index]);
memory.put(pattern, add + minus);
return memory.get(pattern);
}
}
/* EOF */