Leetcode#40Combination Sum II


Combination Sum II

 Total Accepted: 34820 Total Submissions: 138493My Submissions

Question Solution 


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] 

使用堆栈做的,超级不好的方法,没办法啊争取改进

public class Solution {

    

    List<List<Integer>> x=new ArrayList<List<Integer>>();

Map<String, Integer> m=new HashMap<String, Integer>();

void output(Stack<Node> g){

Stack<Node> s=new Stack<Node>();

int[] data=new int[g.size()];

int i=0;

List<Integer> v=new ArrayList<Integer>();

while(!g.isEmpty()){

Node d=g.peek();

//v.add(d.val);

data[i]=d.val;

g.pop();

s.push(d);

i++;

}

while(!s.isEmpty()){

Node d=s.peek();

s.pop();

g.push(d);

}

for(int j=1;j<g.size();j++)

   for(int jj=j;jj>0;jj--)

   {

       if(data[jj]<data[jj-1])

       {

           int mid=data[jj];

           data[jj]=data[jj-1];

           data[jj-1]=mid;

       }

   }

String str=Integer.toString(data[0]);

v.add(data[0]);

for(int j=1;j<g.size();j++)

{

   v.add(data[j]);

   str=str+" "+Integer.toString(data[j]);

}

if(m.get(str)==null)

{

   x.add(v);

   m.put(str,1);

}

}


    public List<List<Integer>> combinationSum2(int[] candidates, int target) {

Stack<Node> stack=new Stack<Node>();

int l=candidates.length;

    int i=0;

    while(i<l)

    {

    if(candidates[i]==target)

    {

    //List<Integer> v=new ArrayList<Integer>();

    //v.add(candidates[i]);

    //x.add(v);

    Node n=new Node(candidates[i], i);

    stack.push(n);

    output(stack);

    i++;

    stack.pop();

    }

    else if(candidates[i]>target)

    {

    i++;

    }

    else

    {

    break;

    }

    }

    if(i==l)

    return x;

   

    Node z=new Node(candidates[i],i);

    stack.push(z);

    int sum=target-z.val;

    while(!stack.isEmpty()){

    Node p=stack.peek();

    int c=p.cur;

    if(sum==0)

    {

    output(stack);

    Node u=stack.peek();

stack.pop();

sum=sum+u.val;

if(u.cur<l-1)

{

int h=u.cur;

while(h+1<l)

{

    if(candidates[h+1]<=sum)

    break;

    else

    h++;

}

    if(h+1<l)

    {

    Node f=new Node(candidates[h+1],h+1);

    stack.push(f);

    sum=sum-candidates[h+1];

    }

    else

    {

    while(!stack.isEmpty())

{

u=stack.peek();

stack.pop();

sum=sum+u.val;

int hh=u.cur;

while(hh+1<l)

{

    if(candidates[hh+1]<=sum)

    break;

    else

    hh++;

}

    if(hh+1<l)

    {

    Node f=new Node(candidates[hh+1],hh+1);

    stack.push(f);

    sum=sum-candidates[hh+1];

    break;

    }

}

    }

}

else

{

while(!stack.isEmpty())

{

u=stack.peek();

stack.pop();

sum=sum+u.val;

int h=u.cur;

while(h+1<l)

{

    if(candidates[h+1]<=sum)

    break;

    else

    h++;

}

    if(h+1<l)

    {

    Node f=new Node(candidates[h+1],h+1);

    stack.push(f);

    sum=sum-candidates[h+1];

    break;

    }

}

}

    }

    else

    {

    while(c+1<l)

{

    if(candidates[c+1]<=sum)

    break;

    else

    c++;

}

    if(c+1<l)

    {

    Node f=new Node(candidates[c+1],c+1);

    stack.push(f);

    sum=sum-candidates[c+1];

    }

    else

    {

    while(!stack.isEmpty())

    {

    Node u=stack.peek();

    stack.pop();

    sum=sum+u.val;

    if(u.cur<l-1)

    {

    int h=u.cur;

    while(h+1<l)

    {

        if(candidates[h+1]<=sum)

        break;

        else

        h++;

    }

        if(h+1<l)

        {

        Node f=new Node(candidates[h+1],h+1);

        stack.push(f);

        sum=sum-candidates[h+1];

        break;

        }

    }  

    }

    }

    }

    }

        return x;

    }

}


class Node{

    int val;

    int cur;

    Node(int c1, int c2){

    val=c1;

    cur=c2;

    }

}


你可能感兴趣的:(Collection,contain,solution,including,positive)