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.

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.

DFS:

public class Solution {
private int cnt = 0;
public int findTargetSumWays(int[] nums, int S) {
if (nums == null)
return 0;
dfs(nums, S, 0, 0);
return cnt;
} public void dfs(int[] nums, int s, int k, int sum) {
if (k == nums.length) {
if (s == sum)
cnt ++;
return ;
}
dfs(nums, s, k+1, sum+nums[k]);
dfs(nums, s, k+1, sum-nums[k]);
}
}

DP:

public class Solution {
public int findTargetSumWays(int[] nums, int s) {
int sum = 0;
for (int n : nums)
sum += n;
// 两种情况找不到结果,找得到的话就用subsetSum去找,证书和是(s + sum) >>> 1,也就是除以2
return sum < s || (s + sum) % 2 > 0 ? 0 : subsetSum(nums, (s + sum) >>> 1);
} public int subsetSum(int[] nums, int s) {
int[] dp = new int[s + 1];
dp[0] = 1;// 初始记录0的位置为1
for (int n : nums)
// 对每个元素,看看他现有能和别的元素相加得到哪些位置的数
for (int i = s; i >= n; i--)
dp[i] += dp[i - n];
return dp[s];
}
}

http://blog.csdn.net/Cloudox_/article/details/64905139?locationNum=1&fps=1

LeetCode-494. Target Sum(DFS&DP)的更多相关文章

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

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

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

  4. LC 494. Target Sum

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

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

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

  6. 【leetcode】494. Target Sum

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

  7. 494. Target Sum - Unsolved

    https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...

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

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

  9. 494. Target Sum

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

随机推荐

  1. Python——eventlet.greenpool

    该模块提供对 greenthread 池的支持. greenthread 池提供了一定数量的备用 greenthread ,有效限制了孵化 greenthread 过多导致的内存不足,当池子中没有足够 ...

  2. 升级win10,提示(RAM)内存不足2G的解决的方法,亲測可行

    前两天升级win10,检測我内存不足2G,可我的电脑是8G的内存如图 百度,google了非常多方法,有些是两根内存,去掉一个就好了,但是我的就一根8G的,其它就没什么好的方法了,改biosCPU选项 ...

  3. R语言 vegan包计算物种累计曲线

    vegan 包是进行群落数据分析最常用的R包,其中的 specaccum 函数用来计算物种的累计曲线 首先看下官方示例: library(vegan) data(BCI) sp1 <- spec ...

  4. [mobile angular ui]MAUI中的font awesome图标

    MAUI中用font awesome替换了glyphicon,但是FA中都有哪些可用的图标呢,在网上搜了一张font awesome的对照表,使用时记着把其中的icon-xxx替换为fa-xxx就可以 ...

  5. 近阶段学习总结(EasyUI的使用)

    最近阶段正在学习Js框架的使用,目前正在详细了解JQuery EasyUI 的使用. jQuery EasyUI 框架帮助我们轻松建立站点: easyui是一个基于jquery的集成了各种用户界面的插 ...

  6. ecshop安装

    ECSHOP开发中心(www.68ecshop.com) 1.下载ecshop程序包,下载地址: http://download.ecshop.com/2.7.3/ECShop_V2.7.3_UTF8 ...

  7. apache 配置会话保持

    1.修改apache_home/conf/httpd.conf,增加以下模块(取消注释,如有其他依赖, 则相应取消注释) LoadModule proxy_module modules/mod_pro ...

  8. [Module] 08 - MVP by Mosby

    From: Mosby MVP使用教程[作者用心] View是消极视图(Passive View), 它尽量不去主动做事, 让Presenter通过抽象方式控制View 例子: 例如Presenter ...

  9. SVN的Hooks功能--强制添加注释

    所谓hooks,可以类似 理解Linux内核Netfilter框架的hook点和hook函数的概念.当用户在维护代码的过程中,其执行的相关动作正好触发了相关hook点,就 会去执行对应hook点的脚本 ...

  10. CentOS 6.4 命令行 安装 VMware Tools

    新建cdrom挂载目录 mkdir /mnt/cdrom 挂载光驱 mount -t auto /dev/cdrom /mnt/cdrom这命令就是把CentOS CDROM挂载在/mnt/cdrom ...