问题描述

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. wordpress爆破脚本的编写

    import requests import sys import queue import threading import time import getopt urll='' users='' ...

  2. [题解] [CF 1250J] The Parade

    题面 题目大意: 给定一个 \(n\) , 所有军人的数量均在 \([1, n]\) 给定 \(a_i\) 代表高度为 \(i\) 的军人的个数 你要将这些军人分成 \(k\) 行, 满足下面两个条件 ...

  3. Linux rpm 安装MySQL

    1 检查是否存在旧版本mysql (1) mysql 执行命令:rpm -qa|grep mysql,若存在旧mysql,删除查询到的旧mysql,执行: rpm -e --nodeps XXXX  ...

  4. legend3---14、所有增删改的前置操作可以是什么

    legend3---14.所有增删改的前置操作可以是什么 一.总结 一句话总结: 先查询数据有没有,这样既起到了服务端验证的功能,也避免了很多增删改可能的错误 1.用户收藏视频,收藏课程有必要建立用户 ...

  5. 解决python 保存json到文件时 中文显示16进制编码的问题

    python 2.7 import codecs import json with codecs.open('Options.json', 'w', encoding='utf-8') as f: j ...

  6. The problem is now the wait_for_fds() example function: it will call something like select(), poll() or the more modern epoll() and kqueue().

    小结: 1.线程与惊群效应 Serializing accept(), AKA Thundering Herd, AKA the Zeeg Problem — uWSGI 2.0 documentat ...

  7. kotlin 泛型约束

    fun <T:Comparable<T>> sort(list :List<T>){} 冒号之后指定的类型就是泛型参数的上界,对于泛型参数T,只允许使用Compar ...

  8. easy-mock 本地部署

    前言 为什么要本地部署 easy-mock呢? easy-mock官网经常挂,太浪费时间了: 公司突然不给上外网,太垃圾了: 就是想要折腾自己,太爱学习了(哈哈哈): Easy-mock 官网 安装需 ...

  9. Linux信号、信号处理和信号处理函数

    信号(signal)是一种软件中断,它提供了一种处理异步事件的方法,也是进程间惟一的异步通信方式.在Linux系统中,根据POSIX标准扩展以后的信号机制,不仅可以用来通知某种程序发生了什么事件,还可 ...

  10. jenkins容器内修改root密码--ubuntu系统

    http://www.voidcn.com/article/p-yvnoogkc-ng.html 由于jenkins官方镜像是ubuntu系统,所有啥的都用 sudo 换到root账号,然后登陆har ...