Leetcode——Target Sum
Question
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.
Solution
这道题相当于找一个子集求和减去剩下的集合求和等于目标值。P表示positive, N表示Negitive
// sum(P) - sum(N) = S
// sum(P) + sum(N) + sum(P) - sum(N) = S + sum(All)
// 2sum(P) = sum(All) + S
// sum(P) = (sum(All) + S) / 2
解题思想:动态规划。dp[j] = dp[j] + dp[j - n]
Code
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
// sum(P) - sum(N) = S
// sum(P) + sum(N) + sum(P) - sum(N) = S + sum(All)
// 2sum(P) = sum(All) + S
// sum(P) = (sum(All) + S) / 2
int sum = 0;
for (int n : nums)
sum += n;
if ((sum + S) & 1 == 1 || sum < S)
return 0;
sum = (sum + S) / 2;
return subsetsum(nums, sum);
}
int subsetsum(vector<int>& nums, int sum) {
int dp[sum + 1] = {0};
dp[0] = 1;
for (int n : nums) {
for (int j = sum; j >= n; j--)
dp[j] += dp[j - n];
}
return dp[sum];
}
};
Leetcode——Target Sum的更多相关文章
- LeetCode Target Sum
原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...
- [LeetCode] 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 ...
- 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 ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [leetcode] Combination Sum and Combination SumII
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
随机推荐
- 06_常用 Linux 命令的基本使用
常用 Linux 命令的基本使用 目标 理解学习 Linux 终端命令的原因 常用 Linux 命令体验 01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命 ...
- android 数据存储<一>----android短信发送器之文件的读写(手机+SD卡)
本文实践知识点有有三: 1.布局文件,android布局有相对布局.线性布局,绝对布局.表格布局.标签布局等,各个布局能够嵌套的. 本文的布局文件就是线性布局的嵌套 <LinearLayout ...
- PythonWeb 开发记录(一)
安装Django Sudo apt-get install python-django 验证安装Django成功 创建Django应用程式的方式 创建的是解决方案 ,然后创建的是Hi 模块 运行项目: ...
- python2与python3之间的主要区别
python2与python3之间的主要区别 print py2:print语句,语句就意味着可以直接跟要打印的东西,如果后面接的是一个元组对象,直接打印 py3:print函数,函数就以为这必须要加 ...
- 使用webpy创建一个简单的restful风格的webservice应用
下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...
- 人活着系列之开会(Floy)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2930 题意:所有点到Z点的最短距离.因为岛名由 ...
- 改善深层神经网络_优化算法_mini-batch梯度下降、指数加权平均、动量梯度下降、RMSprop、Adam优化、学习率衰减
1.mini-batch梯度下降 在前面学习向量化时,知道了可以将训练样本横向堆叠,形成一个输入矩阵和对应的输出矩阵: 当数据量不是太大时,这样做当然会充分利用向量化的优点,一次训练中就可以将所有训练 ...
- 使用 sendKeys(keysToSend) 批量上传文件
未经允许,禁止转载!!! 在selenium里面处理文件上传的时候可以使用sendKeys(keysToSend) 上传文件 例如: element.sendKeys(“C:\\test\\uploa ...
- IIS注册WEB服务扩展
.net framework和iis那个先装的?如果先装IIS,然后再装.net的话,没有问题.但是如果顺序反了的话,需要在命令行里面执行 C:\Windows\Microsoft.NET\Frame ...
- sql server中批量插入与更新两种解决方案分享(存储过程)
转自http://www.shangxueba.com/jingyan/1940447.html 1.游标方式 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONG ...