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 ...
随机推荐
- google浏览器mac上跨域问题解决
open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/ /Use ...
- js-jquery-Validate校验【二】中文api
jQuery.validate 中文 API 名称 返回类型 描述 validate(options) Validator 验证所选的 FORM. valid() Boolean 检查是否验证通过. ...
- Python中的__init__.py的作用
当用 import 导入该目录时,会执行 __init__.py 里面的代码 因此在__init__.py文件中,把深层的包的路径缩短,别的地方就可以在引用到目录级别时引到深层的包.
- C和C++不容易发现的区别
1.char指针指向字符串常量 当下面的代码写到.c文件中时,可以正常运行;而写到.cpp文件中就会报错:无法从“const char [6]”转换为“char *”. char * c = &quo ...
- EL—表达式
El的数据访问操作: 1:获取变量名(四大作用域中的变量?六大作用域?) 只能从这六个区域中拿数据!!! 2:获取对象的属性值 3:获取集合元素 4:执行表达式 1:获取变量名(四大作用域中的变量) ...
- 教你玩转产品管理系统iClap(PC端功能篇)
之前和大家介绍了iClap的基础功能, 这一次针对PC端右侧的工具栏再做一个详细的介绍 随着版本的更新迭代,陆续会有更多工具和功能推出! 导航 为项目成员提供网址浏览访问导航服务,帮助项目成员快速查找 ...
- yii2常用的migrate命令
开发中经常会用到的方法小结: 1../yii migrate xxx_xx 在表中插入某字段 : public function up() {$this->addColumn('{{applic ...
- 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量
这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...
- C++飞机大战
#include<windows.h> #include"resource.h" #include<stdlib.h> #include<time.h ...
- 在outlook中发邮件判断邮件发送成功的方法
早上给企业发了求职邮件,但是发送的时候,自己发现附件比较大,因为之前比较少使用这个工具来发邮件,所以发送之后没有提醒成功与否,求职心切,内心变得比较紧张. 所以查了查方法,发现了以下解决方案,现备注下 ...