Title Link: combination-sum
Import Java.util.arraylist;import java.util.arrays;import java.util.list;/** * Given A set of candidate numbers (C) and a Target number (T), find all unique combinations in C where the candidate numbers sums to t.the same repeated Chosen from C unlimited number of times. Note:all numbers (including target) would be positive integers. Elements in a combination (A1, A2, ..., AK) must is in non-descending order. (ie, a1≤a2≤ ... ≤ak). The solution set must not contain duplicate combinations. For example, given candidate set 2,3,6,7 and Target 7, A solution set is: [7] [2, 2, 3] * */public class Combinationsum { 168/168 test Cases passed.//status:accepted//runtime:274 ms//submitted:7 minutes ago//time complexity O (n!) space complexity O (n) public Li St<list<integer>> sums = new arraylist<list<integer>> (); Public list<list<integer>> combinationsum (int[] candidates, int target) {arrays.sort (candidates); Combinationsum (candidates, 0, new Arraylist<integEr> (), target); return sums; } public void Combinationsum (int[] candidates, Integer begin, list<integer> sum, int target) {if (target = = 0) {sums.add (sum); Return } for (int i = begin; I < candidates.length && target >= candidates[i]; i++) {list<integer> List = New arraylist<integer> (sum); List.add (Candidates[i]) combinationsum (candidates, I, List, target-candidates[i] );} } public static void Main (string[] args) {//system.out.println (Combinationsum (New int[]{2, 3, 6, 7}, 7));}}
Title Link: combination-sum-ii
Import Java.util.arraylist;import java.util.arrays;import java.util.list;/** * 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 LY is used once in the combination. Note:all numbers (including target) would be positive integers. Elements in a combination (A1, A2, ..., AK) must is 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] * */PU Blic class Combinationsumii {//172/172 test Cases passed.//status:accepted//runtime:270 ms//submitted:0 minutes ago// Time complexity O (n!) space complexity O (n) public list<list<integer>> sums = new arraylist<list<integer>> ();p ublic List<list<integer>> combinationSum2 (int[] candidates, int target) {arrays.sort (candidates); CombinationSum2 (candidates, 0, new ArraylIst<integer> (), target); return sums;} public void combinationSum2 (int[] candidates, int begin, list<integer> sum,int target) {if (target = = 0) {Sums.add (s UM); return;} int pre = -1;for (int i = begin; I < candidates.length && Candidates[i] <= target; i++) {//If the current number is the same as the previous number, this Loop Skip directly if (pre = = Candidates[i]) {continue;} list<integer> list = new arraylist<integer> (sum);p re = Candidates[i];list.add (Candidates[i]); CombinationSum2 (candidates, i + 1, list, target-candidates[i]);}} public static void Main (string[] args) {}}
[Leetcode 39&40] Combination Sum I & II