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 中decode函数用法
学习记录: 含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 ...
- xadmin系列之django的url分发的方式
一.先介绍一下我们自己的urls中是如何进行路由分发的 一.一级路由 urlpatterns = [ url(r'^upload/', views.upload,name="upload&q ...
- VS unable to update auto-refresh path。。。。
手工创建提示报错的路径,重新生成,成功
- iOS 10 之后权限设置
iOS 10 之后权限设置 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Privacy - Camera U ...
- c代码片段-注解
#include<stdio.h> /* * int ac 是命令行参数的个数 第一个参数是当前文件地址 * char * arg[] 字符指针的数组, 每一个指针指向一个具体的命令行参数 ...
- Linux磁盘分区与文件系统
一 Linux磁盘分区与文件系统 在Linux中常见的操作系统有:ext2 ext3 ext4 xfs btrfs reiserfs等文件系统的作用主要是明确磁盘或分区上的文件存储方法以及数据结构,L ...
- vuex写法
<template> <div class="hello"> <p>{{count}}</p> <p> <butt ...
- webstorm打开一个门户工程流程
1.电脑上安装了nginx ,进入conf目录下,找到nginx.conf打开 2.将原配置中server部分替换掉 server{ listen 80; server_nam ...
- MongoDb进阶实践之八 MongoDB的聚合初探
一.引言 好久没有写东西了,MongoDB系列的文章也丢下好长时间了.今天终于有时间了,就写了一篇有关聚合的文章.一说到“聚合”,用过关系型数据库的人都应该知道它是一个什么东西.关系型数据库有“聚合” ...
- vs的【warning C4996:'fopen': This function or variable may be unsafe】解决方案
编译警告:warning C4996 与 Security Enhancements in the CRT 将过去的工程用VS2005打开的时候.你有可能会遇到一大堆的警告:warning C4996 ...