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:

  1. Input: nums is [1, 1, 1, 1, 1], S is 3.
  2. Output: 5
  3. Explanation:
  4.  
  5. -1+1+1+1+1 = 3
  6. +1-1+1+1+1 = 3
  7. +1+1-1+1+1 = 3
  8. +1+1+1-1+1 = 3
  9. +1+1+1+1-1 = 3
  10.  
  11. There are 5 ways to assign symbols to make the sum of nums be target 3.

Note:

  1. The length of the given array is positive and will not exceed 20.
  2. The sum of elements in the given array will not exceed 1000.
  3. 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:

  1. class Solution {
  2. public:
  3. int findTargetSumWays(vector<int>& nums, int s) {
  4. int sum = accumulate(nums.begin(), nums.end(), 0);
  5. //(s + sum) & 1,判断s + sum的奇偶;(s + sum) >> 1,即(s + sum)/2
  6. return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1);
  7.  
  8. }
  9. int subsetSum(vector<int>& nums, int s) {
  10. int dp[s + 1] = { 0 };
  11. dp[0] = 1;
  12. for (int n : nums)
  13. for (int i = s; i >= n; i--)
  14. dp[i] += dp[i - n];
  15. return dp[s];
  16. }
  17. };

My Python translation:

  1. import collections
  2. class Solution(object):
  3. def findTargetSumWays(self, nums, S):
  4. """
  5. :type nums: List[int]
  6. :type S: int
  7. :rtype: int
  8. """
  9.  
  10. # DP
  11.  
  12. total = sum(nums)
  13. if (total + S) % 2 != 0:
  14. return 0
  15.  
  16. dp = [0] * (len(nums) + 1)
  17. dp[0] = 1
  18. for n in range(1, len(nums) + 1):
  19. for i in range(S, n + 1, -1):
  20. dp[i] += dp[i-n]
  21.  
  22. return dp[S]

Sol 2:

https://discuss.leetcode.com/topic/76278/concise-python-dp-solution

  1. def findTargetSumWays(self, nums, S):
  2. self.dp = [defaultdict(int) for i in range(len(nums))]
  3. return self.get_ways(nums, S, len(nums)-1)
  4.  
  5. def get_ways(self, nums, S, i):
  6. if i == -1:
  7. return 1 if S == 0 else 0
  8. if S not in self.dp[i]:
  9. self.dp[i][S] = self.get_ways(nums, S + nums[i], i - 1) + self.get_ways(nums, S - nums[i], i - 1)
  10. return self.dp[i][S]

494. Target Sum - Unsolved的更多相关文章

  1. 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 ...

  2. LC 494. Target Sum

    问题描述 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 ...

  3. [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 ...

  4. 494. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  5. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

  6. 【LeetCode】494. Target Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  8. 494 Target Sum 目标和

    给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在你有两个符号 + 和 -.对于数组中的任意一个整数,你都可以从 + 或 -中选择一个符号添加在前面.返回可以使最终数组和为 ...

  9. 【leetcode】494. Target Sum

    题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...

随机推荐

  1. oracle 11g SQL语句补充学习

    添加列: alter table tablename add columnName datatype (not null);        -------需要注意一点的是在添加一列为非空的时候, 表必 ...

  2. 从零开始写bootloader(1)

          下面是具体的代码实现:  #define S3C2440_MPLL_200MHZ ((0x5C<<12)|(0x01<<4)|(0x02)) #define MEM ...

  3. [剑指Offer]快排

    快排 看到一篇博文提到"东拆西补"的思想,非常贴切了. 这里采用传统的方法,没有采用剑指Offer书上的方法. 细节很多,需巩固. 其他知识点 生成一个范围内随机数 见代码,这里为 ...

  4. java测试感受

    这个星期四下午来了一次Java考试,用来测试在暑假自学Java的学习情况,不得不说这次考试十分的成功,把我对这学期的学习信心打击的很难受,我也知道这是我应得的教训,我也对我的专业水平有了很深刻的了解了 ...

  5. 论equals与==不同的重要性

    首先借鉴一下CSDN前辈的总结: 在编程中,通常比较两个字符串是否相同的表达式是“==” ,但在 Java 中不能这么写.在 Java 中,如果要比较 a 字符串是否等于 b 字符串,需要这么写: i ...

  6. 【分布式架构】“spring cloud”与“dubbo”微服务的对比

      秉承站在巨人的肩膀上,同时我也不想重复制造轮子,我发现了一系列关于“分布式架构”方面,我需要,同时能够解决我的一些疑惑.问题的博客,转载过来,原文链接: http://blog.csdn.net/ ...

  7. 被遗忘的having

    清明节后公司网站搞活动主要功能很简单就是实现一个消费送的功能.比如, 当天消费金额满5000 返回10%,5000 及以下 返 7% 的功能.本身这个功能不是很难,但是  这个功能跟上次的一个 新用户 ...

  8. Tomcat配置远程调试端口(windows、Linux)

    当我们需要定位生产环境问题,而日志又不清晰的情况下,我们可以借助Tomcat提供的远程调试,设置如下: // Linxu系统: apach/bin/startup.sh开始处中增加如下内容: decl ...

  9. Java运算符号,对象赋值,别名

    生活发生的一切,才会促使着我继续前行,今天继续更新哦,看书中的代码练习. 例子1  引入net.mindview.util.Print.* ,方便打印结果. package com.date0529; ...

  10. opencv 3.2安装

    opencv 3.2安装 下载地址: https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.2.0/opencv-3.2. ...