问题描述

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.

参考答案

class Solution {
public:
int findTargetSumWays(vector<int>& nums, int s) {
int sum = accumulate(nums.begin(), nums.end(), );
return sum < s || (s + sum) & ? : subsetSum(nums, (s + sum) >> );
} int subsetSum(vector<int>& nums, int s) {
int dp[s + ] = { };
dp[] = ;
for (int n : nums)
for (int i = s; i >= n; i--)
dp[i] += dp[i - n];
return dp[s];
}
};

答案解释

1. (s+sum) & 1 是什么?

通过 &1 操作,检查(s+sum) 是否可以被2整除,只有被整除的数字,才可以继续运算。

2. (s+sum)>>1 是什么?

除以 2 的操作

3. 为什么要有以上操作?

根据 这个论坛 的解释:

              sum(P) - sum(N) = target

    sum(P) + sum(N) + sum(P) - sum(N) = target + sum(P) + sum(N)

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

s+sum 需要被 2 整除。

4. subsetSum 是什么函数?

原理,如下图所示(原创):

LC 494. Target Sum的更多相关文章

  1. LN : leetcode 494 Target Sum

    lc 494 Target Sum 494 Target Sum You are given a list of non-negative integers, a1, a2, ..., an, and ...

  2. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

  3. [LeetCode] 494. Target Sum 目标和

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  4. 494. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  5. 494. Target Sum - Unsolved

    https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...

  6. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  8. 494 Target Sum 目标和

    给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...

  9. 【leetcode】494. Target Sum

    题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...

随机推荐

  1. NSString的创建

    create #progma mark  create NSString void stringCreate(){ //char *s="A String";->c NSSt ...

  2. Servlet容器:Jetty和tomcat的比较

    相同点: Tomcat和Jetty都是一种Servlet引擎,他们都支持标准的servlet规范和JavaEE的规范.不同点: 架构比较Jetty的架构比Tomcat的更为简单Jetty的架构是基于H ...

  3. VS2010,VS2013 Datagridview控件的编辑列功能,弹窗界面被挤扁了

    搜了很久,没找到解决办法,在一个角落看到说要卸载Framework,实践后可以,发出来记一下. 解决办法: 发现自己电脑上多了Framework4.8,可能安装VS2013的时候自动安装的. 卸载了F ...

  4. DNS -- 快速清除DNS缓存

    MAC: sudo dscacheutil -flushcache Linux: dnsmasq的是一个轻量级的DNS.TFTP和DHCP服务器.它的目的是给局域网提供配对的DNS和DHCP服务. d ...

  5. ArcGIS Python 唯一值专题

    import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...

  6. Oracle常用操作表结构的语句

    首先,一起来认识几个单词. alter (改变) rename(重命名) column(柱子,用来表示列) modify(修改) comment on (评论) truncate (删减,截断) 1. ...

  7. HTML、 CSS、 JavaScript三者的关系

    HTML. CSS. JavaScript三者的关系    网页主要由三部分组成: 结构( Structure) . 表现( Presentation) 和行为( Behavior)    HTML ...

  8. BOM相关方法及属性

    browser objec tmodel浏览器对象模型 BOM里面的方法大多在window对象底下,window代表窗口,也就是说,在BOM里面大多调用window下面的东西. 1.open方法是wi ...

  9. 前端知识点回顾——koa和模板引擎

    koa 基于Node.js的web框架,koa1只兼容ES5,koa2兼容ES6及以后. const Koa = requier("koa"); const koa = new K ...

  10. Flask之加载静态资源

    Flask之加载静态资源 1.加载css样式 <link rel="stylesheet" href="{{ url_for('static',filename=' ...