LeetCode-494. Target Sum(DFS&DP)
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)的更多相关文章
- 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 ...
- [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 ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】494. Target Sum
题目如下: 解题思路:这题可以用动态规划来做.记dp[i][j] = x,表示使用nums的第0个到第i个之间的所有元素得到数值j有x种方法,那么很容易得到递推关系式,dp[i][j] = dp[i- ...
- 494. Target Sum - Unsolved
https://leetcode.com/problems/target-sum/#/description You are given a list of non-negative integers ...
- 494. Target Sum 添加标点符号求和
[抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...
- 494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...
随机推荐
- SQLException: Column count doesn't match value count at row 1
INSERT INTO table_name(col_name1, col_name2, col_name3) VALUES('value1','value2'); 语句中,前后列数不等造成的 转自: ...
- Getting Started with Google Guava.pdf
- 安装Java Decompiler
原文:https://blog.csdn.net/yh_zeng2/article/details/75948467 Java Decompiler是Java语言的反编译工具,具体介绍见博客Java ...
- middle源码
CREATE TABLE `middle` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `innserSessionid` VARCHAR(250 ...
- 一键切换hosts文件
1.新建文件host.bat 2.代码 @echo off cd.>C:\Windows\System32\drivers\etc\hosts echo .本地环境 .线上测试环境 ,切换Hos ...
- kafka学习之-雅虎开源管理工具Kafka Manager
http://blog.csdn.net/lizhitao/article/details/44523663
- Android学习之——ListView下拉刷新
背景知识 ListView使用非常广泛,对于使用ListView的应用来说,下拉刷新是必不可少要实现的功能. 我们常用的微博.网易新闻,搜狐新闻都使用了这一功能,如下图所示. 微博 搜狐新闻 ...
- consul在windows下的安装
consul的下载 https://www.consul.io/downloads.html 选择对应的系统版本 修改环境变量path 启动命令窗口 执行命令 consul agent -dev 打开 ...
- redis 的set数据类型
相关命令 1.SADD SADD key-name item1 [item 2…] 将一个或多个成员元素加入到集合中 2.SREM SMEMBERS key-name item1 [item 2…] ...
- 使用System.Web.Optimization对CSS和JS文件合并压缩
在ASP.NET MVC 中JS/CSS文件动态合并及压缩通过调用System.Web.Optimization定义的类ScriptBundle及StyleBundle来实现. 大致步骤如下: 1.A ...