zoukankan      html  css  js  c++  java
  • 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.

    题目含义:给了一个非负数的矩阵,里面的元素是[a0, a1, a2……]和一个目标值S,现在要在这些元素之间加入+或者-,使得里面的元素之间运算结果能够等于目标值S,求一共有多少种加入+或者-的方法

     1 class Solution {
     2     
     3     
     4    int subsetSum(int[] nums, int s) {
     5 //        dp[i]表示子集合元素之和等于当前目标值的方案个数, 当前目标值等于9减去当前元素值
     6 //             当前元素等于1时,   dp[9] = dp[9] + dp[9-1]
     7 //                                dp[8] = dp[8] + dp[8-1]
     8 //                                ...
     9 //                                dp[1] = dp[1] + dp[1-1]
    10 //                当前元素等于2时,dp[9] = dp[9] + dp[9-2]
    11 //                                dp[8] = dp[8] + dp[8-2]
    12 //                                ...
    13 //                                dp[2] = dp[2] + dp[2-2]
    14 //                当前元素等于3时,dp[9] = dp[9] + dp[9-3]
    15 //                                dp[8] = dp[8] + dp[8-3]
    16 //                                ...
    17 //                                dp[3] = dp[3] + dp[3-3]
    18         int[] dp = new int[s + 1];
    19         dp[0] = 1;
    20         for (int n : nums)
    21             for (int i = s; i >= n; i--)
    22                 dp[i] += dp[i - n];
    23         return dp[s];
    24     }
    25     
    26     
    27     //        该方案中数组元素可以分为两组,一组是数字符号为正(P={1,3,5}),另一组数字符号为负(N={2,4})
    28 //        因此: sum(1,3,5) - sum(2,4) = target
    29 //        sum(1,3,5) - sum(2,4) + sum(1,3,5) + sum(2,4) = target + sum(1,3,5) + sum(2,4)
    30 //        2sum(1,3,5) = target + sum(1,3,5) + sum(2,4)
    31 //        2sum(P) = target + sum(nums)
    32 //        sum(P) = (target + sum(nums)) / 2
    33 //        由于target和sum(nums)是固定值,因此原始问题转化为求解nums中子集的和等于sum(P)的方案个数问题
    34     public int findTargetSumWays(int[] nums, int S) {
    35         int sum = 0;
    36         for (int n : nums)
    37             sum += n;
    38         return sum < S || (S + sum) % 2 > 0 ? 0 : subsetSum(nums, (S + sum) / 2);        
    39     }
    40 }

    类似题目:

    377. Combination Sum IV

  • 相关阅读:
    关于GET和POST请求的区别,最通俗全面的回答
    Mac常用命令行
    Jquery中的done() fail() then() $when()到底是什么
    聊聊HTML5中的Web Notification桌面通知
    css实现左右两个div等高
    css样式优先级计算规则
    vue获取后端数据放在created还是mounted方法里面?
    vue的provide和inject特性
    前端路由的实现原理
    Windows鼠标右键新建中增加新建md文件
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7698282.html
Copyright © 2011-2022 走看看