标签:and 状态 including arraylist get rom where 方法 删除
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
题目分析:
测试用例构建
import java.util.*;
class Solution {
List<List<Integer>> list = new ArrayList<>();
List<Integer> arraylist = new ArrayList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
return Sum(candidates,target,0);
}
List<List<Integer>> Sum(int[] candidates, int target,int s) {
if(target <0)
return list;
if(target == 0){
ArrayList<Integer> k = new ArrayList<>(arraylist);
Collections.sort(k);
if(!list.contains(k)){
list.add(new ArrayList<Integer>(k));
}
return list;
}
if(candidates[0]>target){
return list;
}
for(int i = s; i < candidates.length; i++) {
arraylist.add(candidates[i]);
Sum(candidates,target-candidates[i],i);
arraylist.remove(arraylist.size()-1);
}
return list;
}
}
标签:and 状态 including arraylist get rom where 方法 删除
原文地址:https://www.cnblogs.com/clnsx/p/12335212.html