原题链接在这里: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:

  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.

题解:

List some examples. e.g. 1,1,1,1,1.

If all of them are positive, it is 5, all negitative, it is -5. Beyond [-5,5], it can't happen, thus, the ways count is 0.

For this sum question, let dp[i] denotes the sum up to i, the count of ways.

Iterate each num in nums. For num, it could be + or -. say first 1. Then it could -1 or 1.

The ways to -1 is 1, ways to 1 is 1. It is because dp[0] accumlate to dp[1] and dp[-1].

递推时, 上个可能结果或加或减当前num得到新的结果, 不同ways的数目在新结果下累计.  只有对应count大于0时才可能是上个可能结果, because it would not be out of index.

起始值dp[0 + sum] = 1. 可能结果为0的不同ways数目是1. sum is offset. dp[0] means sum up to -sum.

Time Complexity: O(sum * n). sum是nums所有num的和. n = nums.length

Space: O(sum).

AC Java:

 class Solution {
public int findTargetSumWays(int[] nums, int S) {
if(nums == null || nums.length == 0){
return 0;
} int sum = 0;
for(int num : nums){
sum += num;
} if(sum < S || -sum > S){
return 0;
} int [] dp = new int[2*sum+1];
//sum相当于 offset
dp[0+sum] = 1;
for(int num : nums){
int [] next = new int[2*sum+1];
for(int k = 0; k<2*sum+1; k++){
if(dp[k] > 0){
next[k+num] += dp[k];
next[k-num] += dp[k];
}
}
dp = next;
}
return dp[sum + S];
}
}

Method 2:

nums中一部分用的+号 相当于positive, 另一部分用的 - 号相当于negative. 分成两组.

sum(p) - sum(n) = target.

sum(p) + sum(n) + sum(p)-sum(n) = target + sum(p) + sum(n)

2*sum(p) = target + sum(nums)

相当于在nums中找有多少种subarray, subarray自身的和是(target + sum(nums))/2的问题.

subSum求解这个转化问题.

存储到当前数字得到所有结果ways数目. 为什么循环中i要从大到小呢. 这其实是dp的space compression.

update新的dp时会用到上一轮前面的值,所以要从后往前更新. 这样更新时保证用到的都是上轮的值.

Time Complexity: O(sum*nums.length). sum是nums所有num的和.

Space: O(sum).

AC Java:

class Solution {
public int findTargetSumWays(int[] nums, int S) {
int sum = 0;
for(int num : nums){
sum += num;
} if(S < -sum || S > sum || (S+sum)%2 != 0){
return 0;
} return subSum(nums, (S+sum)/2);
} private int subSum(int [] nums, int target){
int [] dp = new int[target+1];
dp[0] = 1;
for(int num : nums){
for(int i = target; i>=num; i--){
dp[i] += dp[i-num];
}
} return dp[target];
}
}

类似Partition Equal Subset Sum.

LeetCode Target Sum的更多相关文章

  1. [LeetCode] Target Sum 目标和

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

  2. Leetcode——Target Sum

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

  3. Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum)

    Leetcode之深度优先搜索(DFS)专题-494. 目标和(Target Sum) 深度优先搜索的解题详细介绍,点击 给定一个非负整数数组,a1, a2, ..., an, 和一个目标数,S.现在 ...

  4. [Leetcode] DP -- Target Sum

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

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

  6. Longest subarray of target sum

    2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...

  7. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  8. [leetcode] Combination Sum and Combination SumII

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

随机推荐

  1. Python 4 函数的参数,内置函数,装饰器,生成器,迭代器,

    一.函数的参数: 1.位置参数:调用函数时根据函数定义的参数位置来传递参数. 2.关键字参数:用于函数调用,通过“键-值”形式加以指定.可以让函数更加清晰.容易使用,同时也清除了参数的顺序需求. 3. ...

  2. ThinkPHP框架基础知识一

    ThinkPHP是一个快速.兼容而且简单的轻量级国产PHP开发框架,诞生于2006年初,原名FCS,2007年元旦正式更名为ThinkPHP,遵循Apache2开源协议发布,从Struts结构移植过来 ...

  3. iMX6 yocto平台QT交叉编译环境搭建

    转:https://blog.csdn.net/morixinguan/article/details/79351909 . /opt/fsl-imx-fb/4.9.11-1.0.0/environm ...

  4. 20145230《JAVA程序设计》第1周学习总结

    20145230<JAVA程序设计>第一周学习总结 教材学习内容总结 在第一周的学习中,我初次认识了JAVA程序的一些基础知识.首先,我们需要在网上先下载JDK或者JDE,通过视频的学习, ...

  5. 用nodejs实现读取文件操作

    //如果不是全局就得引入fs成员 const fs = require("fs"); //fs 核心模块中提供了一个 fs.readFile方法,来读取指定目录下的文件 //fs. ...

  6. Linux下解压分包文件zip(zip/z01/z02)

    分包压缩的zip文件不能被7z解压,且这种格式是Windows才能创建出来,在Linux下不会以这种方式去压包.下面是在Linux下处理这种文件的做法: 方法一: cat xx.z01 xx.zip ...

  7. 服务器windows 2003 安装SQL 2000+SP4

    (在windows 2003安装SQL 2000,对于03系统是没有32位和64位之分的) (32位系统需要重启机器,64不需要) 在站点下载数据库 解压后有两个文件夹 首先安装SQL2000 第1步 ...

  8. freemarker内建函数介绍

    Sequence的内置函数1.sequence?first 返回sequence的第一个值.2.sequence?last 返回sequence的最后一个值.3.sequence?reverse 将s ...

  9. WebUploader API文档

    Web Uploader内部类的详细说明,以下提及的功能类,都可以在WebUploader这个变量中访问到. As you know, Web Uploader的每个文件都是用过AMD规范中的defi ...

  10. python练习_sed替换

    python练习_sed替换 需求: 做一个sed替换小程序,实现在windows下可以与实现linux中sed替换的功能 支持正则(re模块) 以下代码实现的功能与思路: 功能: (1)支持文件内容 ...