Leetcode 题解 - 动态规划-0-1 背包(3):改变一组数的正负号使得它们的和为一给定数 不算动态规划

[LeetCode] Target Sum 目标和

递归help函数  分为help(target - nums[start],start + 1) 和 help(target + nums[start],start - 1)

递归结束条件是start= nums.length  再判断target 等不等于 0

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.

 

这道题给了我们一个数组,和一个目标值,让我们给数组中每个数字加上正号或负号,然后求和要和目标值相等,求有多少中不同的情况。那么对于这种求多种情况的问题,我最想到的方法使用递归来做。我们从第一个数字,调用递归函数,在递归函数中,分别对目标值进行加上当前数字调用递归,和减去当前数字调用递归,这样会涵盖所有情况,并且当所有数字遍历完成后,我们看若目标值为0了,则结果res自增1,参见代码如下:

class Solution {
    private int count = 0;
    public int findTargetSumWays(int[] nums, int S) {
        help(nums, S, 0);
        return count;
    }
    private void help(int[] nums, int S, int start){
        if(nums.length == start){
            if(S == 0)
                count++;
            return;
        }
        help(nums, S - nums[start], start + 1);
        help(nums, S + nums[start], start + 1);
    }
}

错误的解法 res在每次赋值的过成功就是0了 res的改变已经无法体现到实际之中 

class Solution {
    public int findTargetSumWays(int[] nums, int S) {
        int res = 0;
        helper(nums, S, 0, res);
        return res;
    }
    private void helper(int[] nums, int s, int start, int res){
        if(start == nums.length){
            if(s == 0)
                res++;
            return;
        }
        helper(nums, s - nums[start], start + 1, res);
        helper(nums, s + nums[start], start + 1, res);
                 
    }
}   

该问题可以转换为 Subset Sum 问题,从而使用 0-1 背包的方法来求解。

可以将这组数看成两部分,P 和 N,其中 P 使用正号,N 使用负号,有以下推导:

                  sum(P) - sum(N) = target
sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)
                       2 * sum(P) = target + sum(nums)

因此只要找到一个子集,令它们都取正号,并且和等于 (target + sum(nums))/2,就证明存在解。

dp方法

public int findTargetSumWays(int[] nums, int S) {
    int sum = computeArraySum(nums);
    if (sum < S || (sum + S) % 2 == 1) {
        return 0;
    }
    int W = (sum + S) / 2;
    int[] dp = new int[W + 1];
    dp[0] = 1;
    for (int num : nums) {
        for (int i = W; i >= num; i--) {
            dp[i] = dp[i] + dp[i - num];
        }
    }
    return dp[W];
}

private int computeArraySum(int[] nums) {
    int sum = 0;
    for (int num : nums) {
        sum += num;
    }
    return sum;
}

DFS 解法:

public int findTargetSumWays(int[] nums, int S) {
    return findTargetSumWays(nums, 0, S);
}

private int findTargetSumWays(int[] nums, int start, int S) {
    if (start == nums.length) {
        return S == 0 ? 1 : 0;
    }
    return findTargetSumWays(nums, start + 1, S + nums[start])
            + findTargetSumWays(nums, start + 1, S - nums[start]);
}

 

你可能感兴趣的:(leetcode)