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. 关于在IE下JavaScript的 Stack overflow at line 错误可能的原因

    该错误只在IE中出现,出现该提示的原因主要有两种: 1. 重定义了系统的触发事件名称作为自定义函数名如:  onclick / onsubmit …  都是系统保留的事件名称,不允许作为重定义函数名称 ...

  2. RabbitMQ基于Stomp实现与MQTT客户端通信

    请参照RabbitMQ应用和SpringBoot集成RabbitMQ并实现消息确认机制 详情参照官方文档https://www.rabbitmq.com/stomp.html.https://gith ...

  3. 快速的统计千万级别uv

    菜菜,咱们网站现在有多少PV和UV了? Y总,咱们没有统计pv和uv的系统,预估大约有一千万uv吧 写一个统计uv和pv的系统吧 网上有现成的,直接接入一个不行吗? 别人的不太放心,毕竟自己写的,自己 ...

  4. Sass-数据类型

    Sass和JavaScript语言类似,也具有自己的数据类型,在Sass中包含一下几种数据类型 数字:如,1,2,13,10px; 字符串: 有引号字符串或无引号字符串,如,“foo”,"b ...

  5. P与C

    P是排列:与次序有关,P(5.3)=5*4*3 C是组合:与次序无关,C(5.3)=(5*4*3)/(3*2*1)

  6. sql 中 exists用法

    SQL中EXISTS的用法   比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(S ...

  7. spring-boot整合Mybatis多数据源案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...

  8. CodeForces - 849B 几何

    题意:给n个点,问是否能两条平行线覆盖所有的点 思路:因为要求全部覆盖,所以我们第一个点肯定是会入其中一条直线,其实只用判前三个点的所有情况即可 #include<stdio.h> #in ...

  9. 【靶场训练_DVWA】Command Execution

    low 利用: ;ls ../../ 源码分析: <?php if( isset( $_POST[ 'submit' ] ) ) { //将ip对应的值复制给target $target = $ ...

  10. sublime常用基础插件合集

    插件介绍 Package Control 功能:安装包管理简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能使用方法:快捷键 Ctrl+Shift+P,输入 install 选中In ...