首页 > 其他 > 详细

leetcode--Combination Sum II

时间:2014-03-19 16:06:53      阅读:508      评论:0      收藏:0      [点我收藏+]

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 (a1a2, … , 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] 

 

Have you been asked this question in an interview? 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Solution {
    public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
        ArrayList<ArrayList<Integer> > result = new ArrayList<ArrayList<Integer> >();
        Arrays.sort(num);
        int len = num.length;
        if(len > 0 && num[0] <= target){
            ArrayList<ArrayListWithSum> alist = new ArrayList<ArrayListWithSum>();
            alist.add(new ArrayListWithSum(new ArrayList<Integer>(), 0));
            int index = 0;
            while(index < len && !alist.isEmpty()){
                ArrayList<ArrayListWithSum> temp = new ArrayList<ArrayListWithSum>();
                int theSame = checkSame(num, index);
                while(!alist.isEmpty()){            
                    ArrayListWithSum temp1 = alist.remove(0);
                    ArrayList<Integer> toBeAdded = new ArrayList<Integer>();
                    for(int j = 0; j < theSame - index + 1; ++j){
                        ArrayList<Integer> templist = new ArrayList<Integer>();
                        templist.addAll(temp1.li);
                        templist.addAll(toBeAdded);
                        if(temp1.sum + j * num[index] < target)
                            temp.add(new ArrayListWithSum(templist, temp1.sum + j * num[index]));
                        else if(temp1.sum + j * num[index] == target)
                            result.add(templist);
                        toBeAdded.add(num[index]);
                    }
                }
                alist = temp;
                index = theSame;
            }
        }       
        return result;
    }
 
    public static int checkSame(int[] arr, int i){
        int len = arr.length;
        int j = i;
        for(; j < len; ++j){
            if(arr[j] != arr[i])
                break;
        }
        return j;
    }
}
 
class ArrayListWithSum{
    ArrayList<Integer> li;
    int sum;
    ArrayListWithSum(ArrayList<Integer> li, int sum){
        this.li = li;
        this.sum = sum;
    }
}

  

 

leetcode--Combination Sum II,布布扣,bubuko.com

leetcode--Combination Sum II

原文:http://www.cnblogs.com/averillzheng/p/3611474.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!