Algorithm/src/main/java/dynamic_programming/FindTargetSumWays.java
2025-04-13 14:09:25 +08:00

51 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dynamic_programming;
/**
* 题目: 494. 目标和 (FindTargetSumWays)
* 描述:给你一个非负整数数组 nums 和一个整数 target 。
* 向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式
* 例如nums = [2, 1] ,可以在 2 之前添加 '+' ,在 1 之前添加 '-' ,然后串联起来得到表达式 "+2-1" 。
* 返回可以通过上述方法构造的、运算结果等于 target 的不同 表达式 的数目。
示例 2
输入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
* 链接https://leetcode.cn/problems/target-sum/
*/
public class FindTargetSumWays {
public int findTargetSumWays(int[] nums, int target) {
int sum = 0;
for (int num : nums) {
sum += num;
}
// 如果 (sum + target) 不是偶数或者 target 超出 sum 的范围,说明无解
if ((sum + target) % 2 != 0 || Math.abs(target) > sum) {
return 0;
}
// 目标正集合和 P
int P = (sum + target) / 2;
// dp[j] 表示选取若干数凑出和 j 的方案数
int[] dp = new int[P + 1];
dp[0] = 1; // 凑出 0 的和只有一种方案(不选任何数)
// 遍历每个数字
for (int num : nums) {
// 逆序遍历,确保每个数字只被使用一次
for (int j = P; j >= num; j--) {
dp[j] += dp[j - num];
}
}
return dp[P];
}
}