【leetcode】494. Target Sum
题目如下:

解题思路:这题可以用动态规划来做。记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的更多相关文章
- 【LeetCode】494. Target Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】#1 Two Sum
[Question] Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】039. Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all uniq ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
随机推荐
- 在word中的表格指定位置插入一行
//创建一个Document类对象,并加载Word文档 Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administ ...
- 分布式任务队列 Celery
目录 目录 前言 简介 Celery 的应用场景 架构组成 Celery 应用基础 前言 分布式任务队列 Celery,Python 开发者必备技能,结合之前的 RabbitMQ 系列,深入梳理一下 ...
- 【工具安装】VMware 安装教程
介绍:介绍一下 VMware 的安装. 0x01. 下载软件 打开官网 VMware Workstation Pro 点击立即下载即可.  也可以直接使用迅雷,添加下载任务,比浏览器下载速度快些,提 ...
- Delphi驱动方式WINIO模拟按键 可用
http://www.delphitop.com/html/yingjian/152.html Delphi驱动方式WINIO模拟按键 作者:admin 来源:未知 日期:2010/2/1 1:14: ...
- Linux 命令 - man 查看命令的文档
man 命令是 Linux 中最常用的命令,碰到任何让你疑惑的命令,都可以 man 一下来查看详情.不只是 shell 命令,C 语言库函数和系统调用等内容也可以通过 man 命令查看. man 命令 ...
- wsl 下安装docker
docker for windows本身其实是可以直接用的,但是仍然有很多不足,比如说:权限问题.没有docker.sock文件.文件编码问题等.而win10自带的wsl可以非常完美地解决这些问题. ...
- javascript 异常处理
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 更新代码 出现 You need to upgrade the working copy first 错误
今天更新了Eclipse的subclipse插件,更新代码报如下错误: svn: The working copy at 'E:\591woospace\kst_fashion_alipay_v1.2 ...
- 使用 PC 做 FTP/TFTP 服务器,上传和下载文件
使用PC做TFTP服务器,上传和下载文件需要用到一个工具软件,IPOP,可百度下载. 1.在桌面新建一个空闲的文件夹,作为TFTP服务器的存储位置,然后打开IPOP软件,开启服务. 图片中 编号3 的 ...
- MySQL-快速入门(10)触发器
1.什么是触发器 触发器是与表有关的命名数据库对象.触发器由事件来触发某个操作. 触发器是特殊的存储过程,触发器不需要call语句来调用,也不需要手工启动,只需要确定一个预定义的事件发生的时候,就会被 ...