Combination Sum | & || & ||| & IV
Combination Sum |
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
For example, given candidate set 2,3,6,7
and target 7
,
A solution set is: [7]
[2, 2, 3]
Notice
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
given candidate set 2,3,6,7
and target 7
,
A solution set is: [7]
[2, 2, 3]
分析:递归
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
} public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<>(list));
}
helper(index, total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
helper(index + , total, candidates, target, list, listsAll);
}
}
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Notice
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
Given candidate set [10,1,6,7,2,1,5]
and target 8
,
A solution set is:
[
[1,7],
[1,2,5],
[2,6],
[1,1,6]
]
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
} public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<Integer>(list));
}
helper(index + , total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
while (index + < candidates.length && candidates[index] == candidates[index + ]) {
index++;
}
helper(index + , total, candidates, target, list, listsAll);
}
}
Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
分析: 这题和change coin非常相似。
public class Solution {
public int combinationSum4(int[] nums, int target) {
if (nums == null || nums.length == ) return ; int[] dp = new int[target + ];
dp[] = ; for (int i = ; i <= target; i++) {
for (int num : nums) {
if (i - num >= ) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
参考请注明出处:cnblogs.com/beiyeqingteng/
Combination Sum | & || & ||| & IV的更多相关文章
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- leetcode日记 Combination sum IV
题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...
- Leetcode: Combination Sum IV && Summary: The Key to Solve DP
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- Hibernate-清理一级缓存
Session执行一些sql语句把内存中的对象的状态同步到数据库,这个过程被称为session清理. 在默认情况下,Session会在下面的时间点清理缓存. 1 当应用程序调用net.sf.hiber ...
- VS2013打开项目提示此版本的应用程序不支持其项目类型(.csproj)
命令行或者Vs自带的命令提示符输入: devenv.exe /resetskippkgs 重新打开项目即可.
- StyleCop的常见错误
所有规则的翻译(基于版本4.7.44.0): 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialElementsMustB ...
- TinyMCE(富文本编辑器)
[转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 官网演示以及示例代码:https://www.tinymce.com/docs/demo/image-tools/ 转自:http:/ ...
- C语言中常用的string.h的字符函数
strcmp 字符串比较函数 原型: int strcmp(char *str1, char *str2); 例子: ) printf("buffer 1 is greater than b ...
- POJ1364 King
Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...
- eclipse中运行python脚本中有注释为中文的内容,报错:SyntaxError: Non-ASCII character '\xe5'
'''Created on 2015年7月2日 @author: liujuan'''import sysreload(sys) 以上为注释的有个日期中文的,结果运行报错:SyntaxError: N ...
- Spring AOP中定义切点(PointCut)和通知(Advice)
如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...
- Mac OS X 10.9 Mavericks安装后,Xcode调试时模拟器黑屏的处理方法
请耐心的等下去吧,少年! 装了Mac OS X 10.9 Mavericks的同学,如果碰到Xcode调试App时,模拟器黑屏(重置也无效),请耐心的等下去吧,大约10来分钟左右黑屏就会消失,App启 ...
- 检测ADO.net拼接字符串中非法字符
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Refle ...