LeetCode 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.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1a2 ≤ … ≤ ak).
  • 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]

感觉一开始审题未认真,以为给的candidates是一个有序数组,按照非降序排列。等到第一次提交了才知道,给的就是一个无序的set。其实也无所谓。写了个快排,将数组排序。

将每一个元素一次加入到队列中,只与自己和比自己大的数进行叠加,这样可以防止重复。当相加和小于target时,将序列加入到队列中,大于的就不加入,相等的添加结果中。知道队列为空。


public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
       
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(candidates.length==0)
            return result;
        int start = 0;
        int end = candidates.length - 1;
        sort(candidates,start,end);
        if(candidates[0]>target)
            return result;
       
        while(start<=end&&candidates[start]<=target)
        {
            if(candidates[start] == target)
            {
                List<Integer> tem = new ArrayList<Integer>();
                tem.add(candidates[start]);
                result.add(tem);
                break;
            }
            result.addAll(sum(candidates,target,start,end));
            start++;                       
        }
       
       
        return result;
       
    }
   
    public List<List<Integer>> sum(int[] candidates,int target,int start,int end)
    {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        LinkedList<Sequence> queue = new LinkedList<Sequence>();
        Sequence sequ = new Sequence(candidates[start],start);
        queue.add(sequ);
        while(!queue.isEmpty())
        {
            Sequence element = queue.poll();
            for(int i=element.index;i<=end;i++)
            {
                if(element.sum + candidates[i]<target)
                {
                    Sequence temp = new Sequence();
                    List<Integer> list = new ArrayList();
                    list.addAll(element.seq);
                    list.add(candidates[i]);
                    temp.set(element.sum + candidates[i], list,i);
                    queue.addLast(temp);
                }
                else if(element.sum + candidates[i]==target)
                {
                    List<Integer> list = new ArrayList();
                    list.addAll(element.seq);
                    list.add(candidates[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;
    }
   
}

posted on 2014-07-10 11:22  JessiaDing  阅读(191)  评论(0编辑  收藏  举报