Combination Sum:非重复数组中选取若干元素求和等于给定值
2017-10-06 14:03
281 查看
Given a set of candidate numbers (C) (without duplicates) 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
A solution set is:
注意:给顶元素不重复,但是一个元素可以被多次选取
思路:深度优先遍历,注意同意元素可被取多次,所以递归调用时仍从当前元素下表调用,打印数组时要注意每个元素被选取次数。
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
dfs(candidates,0,0,target,list);
return list;
}
static int[] index = new int[10000];
public static void dfs(int[] candidates,int sum,int i,int target,List list){
if(sum>target) return;
if(sum==target){
ArrayList<Integer> l = new ArrayList();
for(int j=0;j<candidates.length;j++){
if(index[j]!=0){
for(int c = index[j];c>0;c--){
l.add(candidates[j]);
}
}
}
list.add(l);
return;
}else{
for(int j = i ;j<candidates.length;j++){
index[j]++;
dfs(candidates,sum+candidates[j],j,target,list);
index[j]--;
}
}
}
}
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] ]
注意:给顶元素不重复,但是一个元素可以被多次选取
思路:深度优先遍历,注意同意元素可被取多次,所以递归调用时仍从当前元素下表调用,打印数组时要注意每个元素被选取次数。
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(candidates);
dfs(candidates,0,0,target,list);
return list;
}
static int[] index = new int[10000];
public static void dfs(int[] candidates,int sum,int i,int target,List list){
if(sum>target) return;
if(sum==target){
ArrayList<Integer> l = new ArrayList();
for(int j=0;j<candidates.length;j++){
if(index[j]!=0){
for(int c = index[j];c>0;c--){
l.add(candidates[j]);
}
}
}
list.add(l);
return;
}else{
for(int j = i ;j<candidates.length;j++){
index[j]++;
dfs(candidates,sum+candidates[j],j,target,list);
index[j]--;
}
}
}
}
相关文章推荐
- Combination Sum II:允许重复的数组中选取若干元素使和为某一给定值
- 给定一个排序数组,删除重复的位置,使每个元素只出现一次,并返回新的长度。
- 每天一道LeetCode-----给定大小为n+1的数组,元素大小在[1 : n]之间,只有一个元素会重复出现多次,找到重复的那个
- 给定一个整型数组,求数组元素大于等于所有元素平均值的元素个数
- 给定一个数组,求数组中所有元素的可能组合(每个元素只出现一次),使其和等于给定数的解决办法
- 在给定数组中,找出最先满足两个数的和等于给定数,输出这两个元素的下标
- php从数组中随机选择若干不重复元素的方法
- 找出和值等于指定值的元素,如有序数组{1,2,6,23,43,64},求和等于25的两个和数则将是元素2和23,时间复杂度是0(n)。
- Java实现——查找数组中连续元素的和等于给定数的子数组
- 给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字。(原数组不必保留)
- leetcode:java.T018_4Sum---给定一个整数数组,找出a + b + c + d = target的唯一解,不能有重复元素组
- 返回两个求和等于给定值的数组下标--算法练习
- 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字
- C++[算法]给定一个具有100个元素的数组,请对该数组随机赋值1-100,不能出现重复的值
- C++编程,输出给定int数组中的重复元素,元素取值有上限
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 求数组中若干个元素之和等于给定值
- python字典分别按照key和value升序或者降序排列--以给定一个含有重复元素的数组list,按照其出现次数大小降序输出
- 从数组和List中随机抽取若干不重复的元素.
- php从数组中随机选择若干不重复元素的方法