问题描述

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. 3.5寸1.44M软盘结构

    结构: 划分: 簇:磁盘驱动器在向磁盘读取和写入数据时,要以扇区为单位.在磁盘上,DOS操作系统是以“簇”为单位为文件分配磁盘空间的.硬盘的簇通常为多个扇区,与磁盘的种类.DOS 版本及硬盘分区的大小 ...

  2. [题解] [SDOI2017] 序列计数

    题面 题解 和 SDOI2015 序列统计 比较像 这个无非就是把乘改成了加, NTT 改成了 MTT 再加上了一个小小的容斥 : 拿所有方案减去不合法方案即可 Code #include <a ...

  3. 微信小程序之--(与唯品会来场粉红色的邂逅 ???)

    Welcome to miaomiaoXiong's segmentfault 微信小程序之--(与唯品会来场粉红色的邂逅 ???) 买买买,虽然双十二刚过,可是唯品会的折扣却是依然火爆.一打开页面, ...

  4. JS函数传递字符串参数(符号转义)

    原文链接:https://blog.csdn.net/Myname_China/article/details/82907965 JS函数传递字符串参数,如果没有转义处理,在接收的时候无法正确的接收字 ...

  5. Windows下MongoDB的安装过程及基本配置

    首先当然是到官网下载 MongoDB 了,点击进入后会看到下载页面,如下图所示: 安装有2种方式: 一种是通过直接点击下载的安装文件进行安装: 另一种是通过命令提示符来安装,下面我将直接通过第一种方式 ...

  6. Intellij IDEA导入JAVA项目并启动(哈哈哈,天天都有人问)

    最近有很多同学,竟然不知道如何使用Intellij IDEA打开Java项目并启动 现在来讲一下,希望不要忘记了 1.打开IDEA开机页面 Maven项目 2.Maven项目是以pom文件引入各项ja ...

  7. ubuntu18.04 systemctl

    systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本.systemd 的特性有:支持并行化任务:同一时候採用 socket 式与 D-Bus 总线式激活服务 ...

  8. sed中使用变量及变量中存在特殊字符‘/’处理

    sed中使用变量,普通的处理方式无法解析变量 如当前file文件中存在字符串pedis,现将其替换为redis [root@localhost work]# cat file pedis 如下两种替换 ...

  9. insmod内核模块时提示Failed to find the folder holding the modules怎么办?

    答:笔者通过重新编译内核和根文件系统解决了此问题 (笔者使用的是openwrt系统) 分析: 1. ’Failed to find the folder holding the modules‘这句l ...

  10. 在arm上执行某个程序时总是提示 not found是怎么回事?

    答: 使用ldd查看程序是否缺少库,如果缺少库,那么就从交叉编译工具链中获取并复制到arm的根文件系统中