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- ...
随机推荐
- oracle 11g SQL语句补充学习
添加列: alter table tablename add columnName datatype (not null); -------需要注意一点的是在添加一列为非空的时候, 表必 ...
- 从零开始写bootloader(1)
下面是具体的代码实现: #define S3C2440_MPLL_200MHZ ((0x5C<<12)|(0x01<<4)|(0x02)) #define MEM ...
- [剑指Offer]快排
快排 看到一篇博文提到"东拆西补"的思想,非常贴切了. 这里采用传统的方法,没有采用剑指Offer书上的方法. 细节很多,需巩固. 其他知识点 生成一个范围内随机数 见代码,这里为 ...
- java测试感受
这个星期四下午来了一次Java考试,用来测试在暑假自学Java的学习情况,不得不说这次考试十分的成功,把我对这学期的学习信心打击的很难受,我也知道这是我应得的教训,我也对我的专业水平有了很深刻的了解了 ...
- 论equals与==不同的重要性
首先借鉴一下CSDN前辈的总结: 在编程中,通常比较两个字符串是否相同的表达式是“==” ,但在 Java 中不能这么写.在 Java 中,如果要比较 a 字符串是否等于 b 字符串,需要这么写: i ...
- 【分布式架构】“spring cloud”与“dubbo”微服务的对比
秉承站在巨人的肩膀上,同时我也不想重复制造轮子,我发现了一系列关于“分布式架构”方面,我需要,同时能够解决我的一些疑惑.问题的博客,转载过来,原文链接: http://blog.csdn.net/ ...
- 被遗忘的having
清明节后公司网站搞活动主要功能很简单就是实现一个消费送的功能.比如, 当天消费金额满5000 返回10%,5000 及以下 返 7% 的功能.本身这个功能不是很难,但是 这个功能跟上次的一个 新用户 ...
- Tomcat配置远程调试端口(windows、Linux)
当我们需要定位生产环境问题,而日志又不清晰的情况下,我们可以借助Tomcat提供的远程调试,设置如下: // Linxu系统: apach/bin/startup.sh开始处中增加如下内容: decl ...
- Java运算符号,对象赋值,别名
生活发生的一切,才会促使着我继续前行,今天继续更新哦,看书中的代码练习. 例子1 引入net.mindview.util.Print.* ,方便打印结果. package com.date0529; ...
- opencv 3.2安装
opencv 3.2安装 下载地址: https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/opencv-3.2. ...