494. Target Sum - Unsolved
https://leetcode.com/problems/target-sum/#/description
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.
Sol 1:
http://blog.csdn.net/u014593748/article/details/70185208?utm_source=itdadao&utm_medium=referral
http://blog.csdn.net/Cloudox_/article/details/64905139?locationNum=1&fps=1
Java:
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int s) {
int sum = accumulate(nums.begin(), nums.end(), 0);
//(s + sum) & 1,判断s + sum的奇偶;(s + sum) >> 1,即(s + sum)/2
return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1); }
int subsetSum(vector<int>& nums, int s) {
int dp[s + 1] = { 0 };
dp[0] = 1;
for (int n : nums)
for (int i = s; i >= n; i--)
dp[i] += dp[i - n];
return dp[s];
}
};
My Python translation:
import collections
class Solution(object):
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
""" # DP total = sum(nums)
if (total + S) % 2 != 0:
return 0 dp = [0] * (len(nums) + 1)
dp[0] = 1
for n in range(1, len(nums) + 1):
for i in range(S, n + 1, -1):
dp[i] += dp[i-n] return dp[S]
Sol 2:
https://discuss.leetcode.com/topic/76278/concise-python-dp-solution
def findTargetSumWays(self, nums, S):
self.dp = [defaultdict(int) for i in range(len(nums))]
return self.get_ways(nums, S, len(nums)-1) def get_ways(self, nums, S, i):
if i == -1:
return 1 if S == 0 else 0
if S not in self.dp[i]:
self.dp[i][S] = self.get_ways(nums, S + nums[i], i - 1) + self.get_ways(nums, S - nums[i], i - 1)
return self.dp[i][S]
494. Target Sum - Unsolved的更多相关文章
- 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 ...
- [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 ...
- 494. 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 添加标点符号求和
[抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...
- 【LeetCode】494. Target Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- 494 Target Sum 目标和
给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...
- 【leetcode】494. Target Sum
题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...
随机推荐
- html position定位
一.fixed居中 css样式代码:{ position:fixed left: 0; right: 0; margin:0 auto; width:300px } 二.Position属性有四个值: ...
- docker使用以及dockerfile编写
一 docker常用命令 1. service docker start 2. docker images 显示所有镜像 3. docker ps [-a] 显示正在运 ...
- Jasperreport5.6.9-----2
Jasperreport5.6.0生成PDF 上一篇讲的是jasperreport5.6.0生成pdf,运行后可以生成pdf,可是和我们的需求有点差距,我们是要求生成后,可以直接打开或保存,这就需 ...
- TZOJ 4712 Double Shortest Paths(最小费用最大流)
描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...
- HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)
描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...
- linux详解sudoers
sudo使用 Linux是多用户多任务的操作系统, 共享该系统的用户往往不只一个.出于安全性考虑, 有必要通过useradd创建一些非root用户, 只让它们拥有不完全的权限; 如有必要,再来提升权限 ...
- how2j学习日志——J2EE(2018年3月28日)
1. 开始跟着站长学习J2EE,首页是简单的Tomcat安装和部署,我从官网上下载的是7.0.85版本,修改server.xml中的默认端口号为80.80端口是web服务的默认端口,因此在浏览器上输入 ...
- cf相关命令
进行登录的命令: cf login -a https://api.bupaas.citicsinfo.com --skip-ssl-validation 进行发布的命令: cf push gwdemo ...
- go语言template包中模板语法总结
package main; import ( "html/template" "os" "fmt" ) type Person struct ...
- OCSP
一.简介 二.协议 三.其他 1)OCSP装订 https://zh.wikipedia.org/wiki/OCSP%E8%A3%85%E8%AE%A2