首页 > 代码库 > LeetCode: Combination Sum II
LeetCode: 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.
Note:
- 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.
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]
其实和Combination Sum 1原理一样。只是在处理细节的时候要考虑到前后数值相同造成的重复。
public class Solution {
public List<List<Integer>> combinationSum2(int[] num, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int start = 0;
int end = num.length - 1;
sort(num,start,end);
if(num[0]>target)
return result;
while(start<=end&&num[start]<=target)
{
if(num[start] == target)
{
List<Integer> tem = new ArrayList<Integer>();
tem.add(num[start]);
result.add(tem);
break;
}
if(start==0)
{
result.addAll(sum(num,target,start,end));
}
else if(start>0&&num[start] != num[start -1])
{
result.addAll(sum(num,target,start,end));
}
start++;
}
return result;
}
public List<List<Integer>> sum(int[] num,int target,int start,int end)
{
List<List<Integer>> result = new ArrayList<List<Integer>>();
LinkedList<Sequence> queue = new LinkedList<Sequence>();
Sequence sequ = new Sequence(num[start],start);
queue.add(sequ);
while(!queue.isEmpty())
{
Sequence element = queue.poll();
for(int i=element.index + 1;i<=end;i++)
{
if(i == element.index + 1)
{
if(element.sum + num[i]<target)
{
Sequence temp = new Sequence();
temp.sum = element.sum + num[i];
temp.seq.addAll(element.seq);
temp.seq.add(num[i]);
temp.index = i;
queue.addLast(temp);
}
else if(element.sum + num[i]==target)
{
List<Integer> list = new ArrayList<Integer>();
list.addAll(element.seq);
list.add(num[i]);
result.add(list);
}
else
{
break;
}
}
else if(i > element.index + 1&& num[i]!=num[i-1])
{
if(element.sum + num[i]<target)
{
Sequence temp = new Sequence();
temp.sum = element.sum + num[i];
temp.seq.addAll(element.seq);
temp.seq.add(num[i]);
temp.index = i;
queue.addLast(temp);
}
else if(element.sum + num[i]==target)
{
List<Integer> list = new ArrayList<Integer>();
list.addAll(element.seq);
list.add(num[i]);
result.add(list);
}
else
{
break;
}
}
}
}
return result;
}
public int partition(int[] sortArray,int low,int hight)
{
int key = sortArray[low];
while(low<hight)
{
while(low<hight && sortArray[hight]>=key)
hight--;
sortArray[low] = sortArray[hight];
while(low<hight && sortArray[low]<=key)
low++;
sortArray[hight] = sortArray[low];
}
sortArray[low] = key;
return low;
}
public void sort(int[] sortArray,int low,int hight)
{
if(low<hight)
{
int result = partition(sortArray,low,hight);
sort(sortArray,low,result-1);
sort(sortArray,result+1,hight);
}
}
}
class Sequence
{
List<Integer> seq = new ArrayList();
int sum;
int index;
public Sequence()
{
sum = 0;
}
public Sequence(int num,int index)
{
sum = num;
seq.add(num);
this.index = index;
}
public void set(int sum,List<Integer> list,int index)
{
seq = list;
this.sum = sum;
this.index = index;
}
}