Combination Sum II:允许重复的数组中选取若干元素使和为某一给定值
2017-10-06 13:38
435 查看
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
A solution set is:
例如1,1,1,1 target为2,输出[1,1]
思路:按照dfs遍历。麻烦的是元素中可能存在重复的元素,但结果里去不能有重复的结果。所以两种办法:第一种是穷举所有结果,再去重;第二种是便利时就不考虑重复结果。第二种更方便一些。
下图是一个例子,圆圈与方框是表示当前已保存的元素,三角是正在遍历的元素,圆圈与方框中元素相同即被看成是同一种情况。
class Solution {
public List<List<Integer>> combinationSum2(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){
l.add(candidates[j]);
}
}
list.add(l);
return;
}else{
for(int j = i ;j<candidates.length;j++){
if(j>i&&candidates[j]==candidates[j-1]) continue;
index[j]++;
dfs(candidates,sum+candidates[j],j+1,target,list);
index[j]--;
}
}
}
}
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,1,1 target为2,输出[1,1]
思路:按照dfs遍历。麻烦的是元素中可能存在重复的元素,但结果里去不能有重复的结果。所以两种办法:第一种是穷举所有结果,再去重;第二种是便利时就不考虑重复结果。第二种更方便一些。
下图是一个例子,圆圈与方框是表示当前已保存的元素,三角是正在遍历的元素,圆圈与方框中元素相同即被看成是同一种情况。
class Solution {
public List<List<Integer>> combinationSum2(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){
l.add(candidates[j]);
}
}
list.add(l);
return;
}else{
for(int j = i ;j<candidates.length;j++){
if(j>i&&candidates[j]==candidates[j-1]) continue;
index[j]++;
dfs(candidates,sum+candidates[j],j+1,target,list);
index[j]--;
}
}
}
}
相关文章推荐
- Combination Sum:非重复数组中选取若干元素求和等于给定值
- leetcode:java.T018_4Sum---给定一个整数数组,找出a + b + c + d = target的唯一解,不能有重复元素组
- Combination Sum II 无序数组中找组合(每个元素只能用一次)使得和为target@LeetCode@LeetCode
- LeetCode—*Combination Sum II(DFS算法C数组中有重复值)
- 219. Contains Duplicate II (判断数组中是否有重复元素且其下标之差不超过k)
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
- 给定一个经过一次旋转的有序数组,从中查找一个值,若存在返回它的索引,不存在返回-1,假定数组存在重复元素
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- Combination Sum II 全排列求和无重复
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
- 167 Two Sum II (给定数组求相加为定值的两个数)
- 154.leetcode Find Minimum in Rotated Sorted Array II(hard)[排序数组 重复元素 二分查找]
- 从数组和List中随机抽取若干不重复的元素.
- C++ 算法 查找旋转数组中的最小值 允许重复元素
- 算法:给定整数型数组,要求去除重复元素形成新数组,并返回新数组长度
- C++[算法]给定一个具有100个元素的数组,请对该数组随机赋值1-100,不能出现重复的值
- Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode
- LeetCode | Combination Sum II(元素的和---2)
- lintcode&九章算法——101. 删除有序数组里的重复元素 II ? 待解决
- 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字