LeetCode Combination Sum

來源:互聯網
上載者:User

標籤:des   os   art   io   for   re   

因為實驗室項目好久沒刷題了。從今天開始重新開始刷題。

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, a1 ≤ a2 ≤ … ≤ 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;
    }
   
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.