Leetcode combination sum

Source: Internet
Author: User
Tags addall

This is because the lab project has not been reviewed for a long time. Start to refresh questions from today.

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

TheSameRepeated number may be chosen fromCUnlimited 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 set2,3,6,7And target7,
A solution set is:
[7]
[2, 2, 3]

I felt that my questions were not carefully reviewed at the beginning. I thought the candidates is an ordered array, which is arranged in non-descending order. It will not be known until it is submitted for the first time. It gives an unordered set. It doesn't matter. Write a quick row to sort the array.

Add each element to the queue at a time, and only overlay it with the number of itself and the number larger than itself. This prevents duplication. When the addition is less than the target, the sequence is added to the queue. If the value is greater than, the sequence is not added. Equal addition results are included. The queue is empty.

Public class solution {
Public 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 <integer> sum (INT [] candidates, 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 (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

{

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.