友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

494. Target Sum

You are given a list of non-negative integers, a1, a2, …​, an, and a target, S. Now you have 2 symbols ` and `-`. For each integer, you should choose one from ` and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:
Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation:

-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

There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.

  2. The sum of elements in the given array will not exceed 1000.

  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

解题分析

这道题可以使用 DFS 解决。不过,需要注意的是,只需要保存关键变量即可。另外,也可以增加实例变量。

思考题

  1. 再推敲一下动态规划的解法;

  2. 学习背包问题!

参考资料

You are given a list of non-negative integers, a1, a2, …​, an, and a target, S. Now you have 2 symbols ` and `-`. For each integer, you should choose one from ` and - as its new symbol.

Find out how many ways to assign symbols to make sum of integers equal to target S.

Example 1:

Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation:

-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

There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.

  2. The sum of elements in the given array will not exceed 1000.

  3. Your output answer is guaranteed to be fitted in a 32-bit integer.

 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
/**
 * Runtime: 3 ms, faster than 90.55% of Java online submissions for Target Sum.
 * Memory Usage: 38.8 MB, less than 10.00% of Java online submissions for Target Sum.
 *
 * Copy from: https://leetcode-cn.com/problems/target-sum/solution/dong-tai-gui-hua-ji-bai-liao-98de-javayong-hu-by-r/[动态规划击败了98%的java用户 - 目标和 - 力扣(LeetCode)]
 */
public int findTargetSumWays(int[] nums, int S) {
    int sum = 0;
    for (int num : nums) {
        sum += num;
    }
    if (sum < S || (sum + S) % 2 == 1) {
        return 0;
    }
    int target = (sum + S) / 2;
    int[] dp = new int[target + 1];
    dp[0] = 1;
    for (int num : nums) {
        for (int i = target; i >= num; i--) {
            dp[i] += dp[i - num];
        }
    }
    return dp[target];
}

/**
 * Runtime: 688 ms, faster than 5.06% of Java online submissions for Target Sum.
 * Memory Usage: 39 MB, less than 10.00% of Java online submissions for Target Sum.
 */
private int count = 0;

public int findTargetSumWaysDfs(int[] nums, int S) {
    if (Objects.isNull(nums) || nums.length == 0) {
        return 0;
    }
    findTargetSumWays(nums, S, 0, 0);
    return count;
}

private void findTargetSumWays(int[] nums, int target, int sum, int index) {
    if (index == nums.length) {
        if (sum == target) {
            count++;
        }
    } else {
        int num = nums[index];
        findTargetSumWays(nums, target, sum + num, index + 1);
        findTargetSumWays(nums, target, sum - num, index + 1);
    }
}