[LeetCode-interview algorithm classic-Java implementation] [216-Combination Sum III (Sum of the number of combinations)], leetcode -- java
[216-Combination Sum III (Sum of the number of combinations )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Download the Code [https://github.com/wang-jun-chao]Original question
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination shoshould be a unique set of numbers.
Ensure that numbers within the set are 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]]
Theme
Search for all combinations that satisfy the sum of k numbers and are equal to n. Only numbers 1-9 are allowed, and the numbers in each combination should be unique. Make sure that the numbers in the combination are listed in ascending order.
Solutions
Backtracking
Code Implementation
Algorithm Implementation class
Import java. util. collections; import java. util. using list; import java. util. list; public class Solution {public List <Integer> combinationSum3 (int k, int n) {// used to save all result lists <List <Integer> result = new external List <> (); // used to save the intermediate result List <Integer> list = new vertex List <> (); // if (k> 0 & k <= 9) {solve (k, 1, n, 0, list, result);} // return result ;} /*** solution ** @ param k Number of elements for each solution * @ param cur processes k elements currently * @ param remainder k-cur + sum of 1 element * @ param prevVal value of element cur-1 * @ param list the collection class of elements to be parsed * @ param result the container that saves all results */public void solve (int k, int cur, int remainder, int prevVal, List <Integer> 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 the element value list. add (remainder); // copy the result to the new queue List <Integer> one = new queue list <> (); for (Integer I: List) {one. add (I);} // Save the result. add (one); // Delete the last element and restore the list. remove (list. size ()-1) ;}}// not the last element else if (cur <k) {for (int I = prevVal + 1; I <= 9-(k-cur); I ++) {// Add element list. add (I); // recursive solve (k, cur + 1, remainder-I, I, list, result); // restore list on site. remove (list. size ()-1 );}}}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/48046271]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.