Leetcode: Combination sum II

Source: Internet
Author: User
Tags addall

Given a collection of candidate numbers (C) And a target number (T), Find all unique combinations inCWhere the candidate numbers sumsT.

Each number inCMay only be usedOnceIn 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 set10,1,2,7,6,1,5And target8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]

In fact, the principle of combination sum 1 is the same. We only need to take into account the duplicates caused by the same numbers before and after processing the details.

Public class solution {
Public 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 <integer> sum (INT [] num, int target, int start, int end)
{
List <list <integer> result = new arraylist <list <integer> ();
Queue list <sequence> queue = new queue list <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

{

While (low

Hight --;

Sortarray [low] = sortarray [hight];

While (low Low ++;
Sortarray [hight] = sortarray [low];
}
Sortarray [low] = key;
Return low;
}

Public void sort (INT [] sortarray, int low, int hight)

{

If (low

{

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;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.