Combinations 集合
来源:互联网 发布:分类算法应用 编辑:程序博客网 时间:2024/06/05 07:28
39. 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.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, 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) { Arrays.sort(candidates); List<List<Integer>> result=new ArrayList<List<Integer>>(); getResult(result,new ArrayList<Integer>(),candidates,target,0); return result; } private void getResult(List<List<Integer>> result, List<Integer> cur, int candidates[], int target, int start){ if(target>0){ for(int i=start;i<candidates.length&&candidates[i]<=target;i++){ cur.add(candidates[i]); getResult(result,cur,candidates,target-candidates[i],i); //体现在这里,如果上一次添加了candidates[0]这次还是从[0] 开始可以继续添加 cur.remove(cur.size()-1); } } else if (target==0){ result.add(new ArrayList<Integer>(cur)); } }}
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.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5]
and target 8
,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]
不允许重复使用给定集合中的数字,但如果有两个1 则1可以使用两次
在上述代码中做改变,在添加candidates[i]之后 从i+1开始寻找而不是从i开始 避免重复使用给定数字,在最后添加结果集时检查是否存在重复情况
public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { Arrays.sort(candidates); List<List<Integer>> result=new ArrayList<List<Integer>>(); getResult(result,new ArrayList<Integer>(),candidates,target,0); return result; } private void getResult(List<List<Integer>> result, List<Integer> cur, int candidates[], int target, int start){ if(target>0){ for(int i=start;i<candidates.length&&candidates[i]<=target;i++){ cur.add(candidates[i]); getResult(result,cur,candidates,target-candidates[i],i+1); cur.remove(cur.size()-1); } } else if (target==0){ if(!result.contains(cur)) result.add(new ArrayList<Integer>(cur)); } }}
216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
可以用条件n<0/n<0||k<0 来进行判断
注意在添加list时要 new一个ArrayList 来添加,list是引用类型 用后变空!!
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> result=new ArrayList<List<Integer>>(); List<Integer> list=new ArrayList<Integer>(); getCombination(result,new ArrayList<Integer>(),1,n,k); return result; } public void getCombination(List<List<Integer>> result,List<Integer> list,int start,int n,int k){ if(n<0) return ; if(k==0&&n==0) result.add(new ArrayList<Integer>(list)); for(int i=start;i<=9;i++){ list.add(i); getCombination(result,list,i+1,n-i,k-1); list.remove(list.size()-1); } }}
377. 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 = 4The 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.
Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?
用动态规划的做法:状态转移方程: combinationSum4(x): f(x)
f(target) = f(target - nums[0]) + f(target - nums[1]) + ... + f(target - nums[nums.length-1])
public class Solution { public int combinationSum4(int[] nums, int target) { int []dp=new int[target+1]; Arrays.sort(nums); for(int i=0;i<dp.length;i++){ for(int j=0;j<nums.length;j++){ if(i<nums[j]) break; if(i==nums[j]) dp[i]++; else dp[i]+=dp[i-nums[j]]; } } return dp[target]; }}
仍旧用回溯的做法,超时
public class Solution { public int combinationSum4(int[] nums, int target) { List<List<Integer>> result=new ArrayList<List<Integer>>(); getCom(result,new ArrayList<Integer>(),nums,target); return result.size(); } public void getCom(List<List<Integer>> result,List<Integer> list,int []nums,int target){ if(target<0) return ; if(target==0) result.add(new ArrayList(list)); for(int i=0;i<nums.length;i++){ list.add(nums[i]); getCom(result,list,nums,target-nums[i]); list.remove(list.size()-1); } }}
77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]给定范围和数字个数,返回它们能组成的符合要求的所有集合
从1开始往后找没添加一个就将k-1 直到k=0
回溯 递归
public class Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> combs=new ArrayList<List<Integer>>(); combine(combs,new ArrayList<Integer>(),1,n,k); return combs; } public static void combine(List<List<Integer>> combs,List<Integer> comb,int start,int n,int k){ if(k==0){ combs.add(new ArrayList<Integer>(comb)); return; } for(int i=start;i<=n;i++){ comb.add(i); combine(combs,comb,i+1,n,k-1); comb.remove(comb.size()-1); } }}
- Combinations 集合
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- Combinations
- combinations
- Combinations
- Combinations
- Combinations
- 简单的http协议
- thinkphp初始页
- Nodejs快速入门:PART3
- C++编程入门系列之二(原码、反码与补码)
- iOS 蓝牙开发
- Combinations 集合
- System.out.println("put:"+map.put("01","zhangsan1"));
- Nodejs快速入门:PART4
- 51nod 1272 最大距离(贪心)
- Thinkphp模块配置
- Hibernate框架基础——Hibernate API及Hibernate主配置文件
- Nodejs核心模块 PART1
- HDU5912-Fraction
- gcc4.8.4 升级gcc4.9.4