弟中弟的Leetcode总结——数组类(十二)

弟中弟的Leetcode总结——数组类(十二)

题目描述

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:
The length of the given array is positive and will not exceed 20.
The sum of elements in the given array will not exceed 1000.
Your output answer is guaranteed to be fitted in a 32-bit integer.

思路一——回溯法

题目限制了输入最多20个数,因此使用回溯法不会超时。只需要将数字位置和符号进行递归即可。

代码(C)

int count=0;
void backtrack(int *nums, int pos, int numsSize, int S, int op){
    if(op==0)
        S-=nums[pos];
    if(op==1)
        S+=nums[pos];
    //如果到最后一个数,判断是否等于target
    if(pos==numsSize-1){
        if(S==0){
            count++;
            return;
        }
    }
    else{
        pos++;
        //每次放入两个符号
        backtrack(nums,pos,numsSize,S,0);
        backtrack(nums,pos,numsSize,S,1);
    }
}
int findTargetSumWays(int* nums, int numsSize, int S) {
    count=0;
    //0为+,1为-
    backtrack(nums,0,numsSize,S,0);
    backtrack(nums,0,numsSize,S,1);
    return count;
}

思路二——动态规划

可以这么考虑问题:在给定的数字中找到一个子集,使得另一个子集与这个子集之差为target值。例如
nums=[1,2,3,4,5]target=3
那么一个可能的解是+1-2+3-4+5 = 3。其中,一个子集P为{1,3,5},另一个子集N为{2,4}
因此可以得到P - N = target
两边都加上P + N可以得到P + N + P - N = target + P + N
因此P * 2 = target + sum(nums)
所以问题可以等价为找到一个子集P,使得它等于target加所有数的和的一半,或者说等于一个特定的值。
注意到,target+sum(nums)必须为偶数,所以如果为奇数,则可以直接输出没有解,也就是0。
设数组dp[i][j]表示0i元素中和为j的组合个数,那么可以得到状态转移方程
dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i]]
所以可以得到代码

 for (int i = 0; i < nums.length; i++) dp[i][0] = 1;
 for (int i = 1; i < nums.length; i++)
    //这里需要从大向小循环,否则会重复
    for (int j = s; j >= nums[i]; j--) 
       dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i]]; 
 return dp[nums.length-1][s];

但是,可以看到,其实下标i在循环中是没有用的,因为总是使用的连续的两行,因此可以简化为
dp[j] = dp[j] + dp[j-nums[i]],从而减少一重循环。

代码(C++)

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int s) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1); 
    }   

    int subsetSum(vector<int>& nums, int s) {
        int dp[s + 1] = { 0 };
        dp[0] = 1;
        for (int n : nums)
            for (int i = s; i >= n; i--)
                dp[i] += dp[i - n];
        return dp[s];
    }
};

参考

https://leetcode.com/problems/target-sum/discuss/97334/Java-(15-ms)-C++-(3-ms)-O(ns)-iterative-DP-solution-using-subset-sum-with-explanation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值