题目如下:

解题思路:这题可以用动态规划来做。记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i-1][j - nums[i]] + dp[i-1][j + nums[i]]。考虑到j可以为负数,因为j的取值范围是[-sum(nums) ,sum(nums)],为了保证在dp数组中的j一直为正数,我们做一个数组的向右平移,平移sum(nums)的长度,即把-sum(nums) 移动到0。

代码如下:

class Solution(object):
def findTargetSumWays(self, nums, S):
"""
:type nums: List[int]
:type S: int
:rtype: int
"""
Amount = sum(nums)
if S > Amount or S < -Amount:
return 0
MaxValue = max(nums)
dp = [[0 for x in xrange(2*(Amount+MaxValue)+1)] for x in nums]
dp[0][nums[0] + Amount] += 1
dp[0][-nums[0] + Amount] += 1
for i in xrange(1,len(dp)):
for j in xrange(-Amount,Amount+1):
dp[i][j + Amount] = dp[i - 1][j - nums[i]+ Amount] + dp[i - 1][j + nums[i] + Amount] #print dp return dp[len(nums)-1][S+ Amount]

【leetcode】494. Target Sum的更多相关文章

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

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

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  5. 【LeetCode】#1 Two Sum

    [Question] Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  7. 【LeetCode】039. Combination Sum

    题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...

  8. 【LeetCode】040. Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  9. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. vue 中滚动div至最底部

    scrollToBottom: function () { this.$nextTick(() => { var container = this.$el.querySelector(" ...

  2. P站图片下载工具。

    下载 Pixiv 的图片比较麻烦,就做了这么个东西. 主要就是用 HttpWebRequest HttpWebResponse 下载了网页的 html 代码然后截取里面的内容.代码上传到了文件里. p ...

  3. 一图看懂python对excel的读写

  4. mysql中关于 like ,not like 的用法时不能显示空值的数据(空值不参与判断,直接过滤空值)

    一般在项目中用法是(这种没办法显示空值的数据): select * from 表 where 字段 like %aaa%; 解决空值不显示(除了显示的数据外,空值数据也可以显示): select * ...

  5. Balanced Binary Tree(平衡二叉树)

    来源:https://leetcode.com/problems/balanced-binary-tree Given a binary tree, determine if it is height ...

  6. JAVA第四周总结与实验2

    实验二 Java简单类与对象 一. 实验目的 (1) 掌握类的定义,熟悉属性.构造函数.方法的作用,掌握用类作为类型声明变量和方法返回值: (2) 理解类和对象的区别,掌握构造函数的使用,熟悉通过对象 ...

  7. [转帖]CBO和RBO

    http://www.itpub.net/thread-263395-1-1.html 参数不能随便改啊.. optimizer_features_enable('8.1.7') ORACLE 提供了 ...

  8. Vue 实现手动刷新组件

    Vue 实现手动刷新组件:https://www.jianshu.com/p/742142dc95f3

  9. P2562kitty猫基因

    这道题是安徽NOI省选题,但是难度并没有那么难. 这道题是一个字符串的递归题,有很多大佬用线段树来写的(我也想学线段树,lowbit当头一棒).题意为全部相同则输出B或A,不同则分成长度相同的两个子串 ...

  10. 洛谷 P1484 种树(优先队列,贪心,链表)

    传送门 解题思路 第一眼的贪心策略:每次都选最大的. 但是——不正确! 因为选了第i个树,第i-1和i-1棵树就不能选了.所以,要有一个反悔操作. 选了第i个后,我们就把a[i]的值更新为a[l[i] ...