Level:

  Medium

题目描述:

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.

思路分析:

  题意是让在一个数组中的一些数之前添加“+”,其它的数之前添加“-”,从而让数组之和达到给定的数。

  我们将添加“+”的数放入集合P,其它的数放入集合N,于是我们有:

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

  于是有sum(P) = (target + sum) / 2,那么不妨这样理解题意,从一个数组中选定一些数,使它们的和为sum(P),如此就变成了很经典的0/1背包问题,从一个n大小的背包中选出总和为sum(P)的方案个数。

  状态表示:dp[i] [j]代表前i个数中和为j的方案个数。

  状态转移方程:dp[i] [j] = dp[i-1] [j] + dp[i-1] [j-nums[i]],dp[0] [0] = 1

  返回结果:dp[n] [target],n为数组大小,target为sum(P)。

  如此时间复杂度为O(N^2),空间复杂度为O(M*N)。

代码:

public class Solution{
public int findTargetSumWays(int[] nums, int S){
int sum=0;
for(int i=0;i<nums.length;i++){
sum=sum+nums[i];
}
if(sum<S||S<-sum)
return 0;
int target=(sum+S)/2;
int []dp=new int [target+1]; //dp[i]表示和为 i的方案数。
dp[0]=1;
for(int i=0;i<nums.length;i++){
for(int j=target;j>=nums[i];j--){
dp[j]=dp[j]+dp[j-nums[i]];
}
}
return dp[target];
}
}

59.Target Sum(目标和)的更多相关文章

  1. [LeetCode] Target Sum 目标和

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

  2. 494 Target Sum 目标和

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

  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. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  5. [Leetcode] DP -- Target Sum

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

  6. LeetCode Target Sum

    原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...

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

  8. LC 494. Target Sum

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

  9. Longest subarray of target sum

    2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...

随机推荐

  1. JSONP面试

    jQuery 的 JSONP的原理是动态创建一个 script 标签,利用src 发送请求,获取数据 回调函数的键名叫做  callback 跟ajax没有关系 JSONP:主要是利用 script标 ...

  2. 关于同PC上存在多个版本的GeneXus

    如题 有的时候需要在不同的版本上开发  如我一般 有四个版本IDE 那么有的时候可能在安装的时候 提示安装失败 比如这样 这个时候你需要将安装好的GeneXus安装目录 全部备份一下 然后  从控制面 ...

  3. linux性能分析工具Top

  4. ini配置文件内如果有更新参数

    ini配置文件内如果有更新参数 执行更新 更新参数 自动去下载执行????

  5. 十分钟理解Redux核心思想,过目不忘。

    白话Redux工作原理.浅显易懂. 如有纰漏或疑问,欢迎交流. Redux 约法三章 唯一数据源(state) 虽然redux中的state与react没有联系,但可以简单理解为react组件中的th ...

  6. python爬虫:2.每天爬取数据量是多少?

    带宽 网站阈值 单机 分布式 几百万

  7. springboot+jsp项目实例(第二弹)(成功)

    1.创建spring boot项目,使用idea自带的spring initializr创建Spring boot的maven项目(我是先创建了一个空的项目). 开始创建Spring boot项目,点 ...

  8. node-解压版 安装配置测试

    一.下载node压缩包   地址:https://nodejs.org/en/download/ 二.解压下载的压缩包,在文件根目录新增两个文件夹: node_cache:缓存文件位置 node_gl ...

  9. 如何做好APP功能测试?

    一.如何做好app的测试工作? 22 个回答  斗魂大陆 凡是可能会出错的地方,一定会出错!--墨菲法则 腾讯有个平台可以实现适配兼容.服务器压力.性能测试.弱网络.耗电量测试等等,挺全面的.WeT ...

  10. Word中页码及目录、参考文献的制做方法

    1.页码从正文开始 1.要想从哪里显示第一页,就在这页的前一页的最后一行最后的地方,插入分隔符---下一页2.然后在你想显示第一页的那一页双击页脚处,点击取消掉“链接到前一条页眉”.(这是为了取消原来 ...