给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S。现在你有两个符号 + 和 -。对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面。
返回可以使最终数组和为目标数 S 的所有添加符号的方法数。
示例 1:
输入: nums: [1, 1, 1, 1, 1], S: 3
输出: 5
解释:
-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
一共有5种方法让最终目标和为3。
注意:
    1.数组的长度不会超过20,并且数组中的值全为正数。
    2.初始的数组的和不会超过1000。
    3.保证返回的最终结果为32位整数。
详见:https://leetcode.com/problems/target-sum/description/

C++:

方法一:

class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
int res=0;
helper(nums,S,0,res);
return res;
}
void helper(vector<int> &nums,int S,int start,int &res)
{
if(start>=nums.size())
{
if(S==0)
{
++res;
}
return;
}
helper(nums,S-nums[start],start+1,res);
helper(nums,S+nums[start],start+1,res);
}
};

方法二:

class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S)
{
int n = nums.size();
vector<unordered_map<int, int>> dp(n + 1);
dp[0][0] = 1;
for (int i = 0; i < n; ++i)
{
for (auto &a : dp[i])
{
int sum = a.first, cnt = a.second;
dp[i + 1][sum + nums[i]] += cnt;
dp[i + 1][sum - nums[i]] += cnt;
}
}
return dp[n][S];
}
};

方法三:

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

参考:https://www.cnblogs.com/grandyang/p/6395843.html?utm_source=itdadao&utm_medium=referral

https://blog.csdn.net/hit0803107/article/details/54894227

494 Target Sum 目标和的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LC 494. Target Sum

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

  4. [LeetCode] 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 添加标点符号求和

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

  6. 494. Target Sum

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

  7. 494. Target Sum - Unsolved

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

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

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

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

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

随机推荐

  1. curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused

    今天我用curl命令,无论如何都是出现: curl: (7) Failed to connect to 127.0.0.1 port 1086: Connection refused 找了很久,不知道 ...

  2. FLTK 简介

          FLTK,如同其名字所表达的:The Fast Light Tool Kit,一个轻量级的GUI开发库.但这轻量级并不代表功能的羸弱,相反,FLTK在具有基本的GUI功能之外,还拥有一些特 ...

  3. iOS内存管理机制解析之MRC手动引用计数机制

    前言: iOS的内存管理机制ARC和MRC是程序猿參加面试基本必问的问题,也是考察一个iOS基本功是 否扎实的关键,这样深入理解内存管理机制的重要性就不言而喻了. iOS内存管理机制发展史 iOS 5 ...

  4. link与import区别

    本质上,这两种方式都是为了加载css文件,但还是存在细微的差别. 差别1:老祖宗的差别,link属于XHTML标签,而@import完全是css提供的一种方式. link标签除了可以加载css外,还可 ...

  5. Objective-C 中Socket常用转换机制(NSData,NSString,int,Uint8,Uint16,Uint32,byte[])

    最近项目中要用到socket通讯,由于涉及到组包问题,所以需要数据类型之间的来回转换,现在分享出来 如果想要请教Socket的问题请留言,我会随时回答的 1. int类型转16进制hexstring ...

  6. hdu-5718 Oracle(水题)

    题目链接: Oracle Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others) ...

  7. ImportError: No module named 'httplib'

    原因:Python 2.x中的"httplib"模块在Python 3.x中变成了"http.client" 原代码: import httplib impor ...

  8. Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar

    转自:https://blog.csdn.net/u011422744/article/details/39851693 在SSH开发,搭建环境的时候,启动tomcat服务器,就报这个异常! 信息: ...

  9. div 加滚动条的方法

    div 加滚动条的方法: <div style="position:absolute; height:400px; overflow:auto"></div> ...

  10. bzoj 4530: [Bjoi2014]大融合【LCT】

    新姿势,一般来讲LCT只能维护splay重边里的数据,而这里要求维护整颗子树的size 多维护一个sq表示当前点轻儿子的size和,si表示包括轻重边的整颗子树的大小 然后需要改sq的地方是link和 ...