"Leetcode-Interview algorithm classic-java Implementation" "216-combination Sum III (combined number of sums)"

Source: Internet
Author: User

"216-combination Sum III (combined number of sums)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original Question

Find all possible combinations of k numbers this add up to a number n, given this only numbers from 1 to 9 can be used and Each combination should is a unique set of numbers.
Ensure that numbers within the set is sorted in ascending order.
Example 1:
Input:k = 3, n = 7
Output:
  [[1,2,4]]
Example 2:
Input:k = 3, n = 9
Output:
  [[1,2,6], [1,3,5], [2,3,4]]

Main Topic

Look for all combinations that satisfy the sum of K equals n, only the number 1-9 is allowed, and the numbers in each combination should be unique. Make sure the numbers in the group are in ascending order.

Thinking of solving problems

Backtracking method

Code Implementation

Algorithm implementation class

ImportJava.util.Collections;ImportJava.util.LinkedList;ImportJava.util.List; Public  class solution {     PublicList<list<integer>>combinationSum3(intKintN) {//For saving all resultslist<list<integer>> result =NewLinkedlist<> ();//For saving intermediate resultslist<integer> list =NewLinkedlist<> ();//Conditions are met on the problem solving operation        if(k >0&& k <=9) {Solve (k,1N0, list, result); }//Return results        returnResult }/** * Solution Method * * @param k number of elements per solution * @param cur currently processing k elements * @par     Am remainder K-cur + 1 elements and * @param Prevval The value of the cur-1 element * @param List of elements to be solved in the collection class * @param result save container for all results * /     Public void Solve(intKintCurintRemainder,intPrevval, list<integer> List, list<list<integer>> result) {//Process the last element        if(cur = = k) {//remainder is the value of the last solution element, it must be greater than the value of the previous solution element and cannot exceed 9            if(Remainder > Prevval && remainder <=9) {//Add element valueList.add (remainder);//Copy results to a new queueList<integer> one =NewLinkedlist<> (); for(Integer i:list)                {One.add (i); }//Save ResultsResult.add (one);//Delete the last element for site restoreList.remove (List.size ()-1); }        }//Not the last element        Else if(Cur < k) { for(inti = Prevval +1; I <=9-(K-cur); i++) {//Add elementList.add (i);//Recursive solutionSolve (k, cur +1, Remainder-i, I, list, result);//Field restoreList.remove (List.size ()-1); }        }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48046271"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "216-combination Sum III (combined number of sums)"

Related Article

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.