59.Target Sum(目标和)
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:
- The length of the given array is positive and will not exceed 20.
- The sum of elements in the given array will not exceed 1000.
- 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(目标和)的更多相关文章
- [LeetCode] Target Sum 目标和
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
- 494 Target Sum 目标和
给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...
- [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 ...
- Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)
Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...
- [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 ...
- LeetCode Target Sum
原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...
- 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 ...
- LC 494. Target Sum
问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
随机推荐
- 搭建阿里云服务器(centos,jdk和Tomcat版本)
1.购买服务器(登录阿里云,购买服务器,并进入控制台,查看自己的服务器实例 2.域名注册(这步可以省略,直接IP地址访问,因为域名需要备案),购买域名的需要进行解析以及绑定自己的服务器 3.可以准备一 ...
- 创建一个java项目并部署到weblogic服务器
转自:https://blog.csdn.net/krystal_sl/article/details/52847953 新建一个项目的步骤 打开eclipse,右键点击new–>java pr ...
- shell变量的声明和使用
- Git--02 Devops介绍及git安装部署
目录 1. Devops介绍 01. 运维介绍 02. Devops是什么 03. Devops能干嘛 04. Devops如何实现 2. Git版本控制系统 01. 版本控制系统简介 02. 为什么 ...
- python发行包 IDE
https://blog.csdn.net/qq_38188725/article/details/80624004 https://blog.csdn.net/qq_38188725/article ...
- CentOS 7.4 安装python3及虚拟环境
[转]:https://www.centos.bz/2018/05/centos-7-4-%e5%ae%89%e8%a3%85python3%e5%8f%8a%e8%99%9a%e6%8b%9f%e7 ...
- Center OS7网络设置
虚拟机上设置网络连接为NAT方式(两层路由) 1:保证windows NAT 和dhcp服务启动 2:/etc/sysconfig/network-scripts/ifcfg-* TYPE=Ether ...
- Codefroces 958C2 - Encryption (medium) 区间dp
转自:https://www.cnblogs.com/widsom/p/8857777.html 略有修改 题目大意: n个数,划分为k段,每一段的和mod p,求出每一段的并相加,求最大是多 ...
- Erlang/Elixir精选-第4期(20191223)
精选文章 A digital symphony - The architecture of API Fortress. 使用Actor模型来支持基于微服务的大规模分布式软件架构.用实例解释了Actor ...
- Appium API文档中文版
传送门 https://testerhome.com/topics/3144