-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0494.目标和.java
105 lines (95 loc) · 2.35 KB
/
0494.目标和.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.HashMap;
import java.util.Map;
/*
* @lc app=leetcode.cn id=494 lang=java
*
* [494] 目标和
*
* https://leetcode.cn/problems/target-sum/description/
*
* algorithms
* Medium (49.09%)
* Likes: 1407
* Dislikes: 0
* Total Accepted: 287.1K
* Total Submissions: 584.4K
* Testcase Example: '[1,1,1,1,1]\n3'
*
* 给你一个整数数组 nums 和一个整数 target 。
*
* 向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :
*
*
* 例如,nums = [2, 1] ,可以在 2 之前添加 '+' ,在 1 之前添加 '-' ,然后串联起来得到表达式 "+2-1" 。
*
*
* 返回可以通过上述方法构造的、运算结果等于 target 的不同 表达式 的数目。
*
*
*
* 示例 1:
*
*
* 输入:nums = [1,1,1,1,1], target = 3
* 输出:5
* 解释:一共有 5 种方法让最终目标和为 3 。
* -1 + 1 + 1 + 1 + 1 = 3
* +1 - 1 + 1 + 1 + 1 = 3
* +1 + 1 - 1 + 1 + 1 = 3
* +1 + 1 + 1 - 1 + 1 = 3
* +1 + 1 + 1 + 1 - 1 = 3
*
*
* 示例 2:
*
*
* 输入:nums = [1], target = 1
* 输出:1
*
*
*
*
* 提示:
*
*
* 1
* 0
* 0
* -1000
*
*
*/
// @lc code=start
class Solution {
// public int findTargetSumWays(int[] nums, int target) {
// return findTargetSumWaysHelper(nums, target, 0);
// }
// private int findTargetSumWaysHelper(int[] nums, int target, int index) {
// if (index == nums.length - 1)
// return Math.abs(target) == Math.abs(nums[index]) ? (target == 0 ? 2 : 1) : 0;
// // 第一个数前面添加 +
// int positive = findTargetSumWaysHelper(nums, target - nums[index], index +
// 1);
// // 第一个数前面添加 -
// int negative = findTargetSumWaysHelper(nums, target + nums[index], index +
// 1);
// return positive + negative;
// }
public int findTargetSumWays(int[] nums, int target) {
int sum = 0, diff = 0;
for (int num : nums)
sum += num;
diff = sum - target;
if (diff < 0 || diff % 2 != 0)
return 0;
int negative = diff / 2;
int[] dp = new int[negative + 1];
dp[0] = 1;
for (int num : nums) {
for (int i = negative; i >= num; i--)
dp[i] += dp[i - num];
}
return dp[negative];
}
}
// @lc code=end