标签:font convert number arraylist eve max 2.0 can new
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target){
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
helper(target, candidates, 0, cur, result);
return result;
}
private void helper(int target, int[] coins, int index, List<Integer> cur, List<List<Integer>> result){
if(index == coins.length - 1){
if( target % coins[coins.length - 1] == 0){
cur.add(target / coins[coins.length - 1]);
List<Integer> right_format = another_one(coins, cur);
result.add(new ArrayList<Integer>(right_format));
cur.remove(cur.size() - 1); //this removes the freq of last number if its a valid solution
} // for example, cur = (0, 1) and its a valid solution, remove 1 to back to cur = (0), which is
// previous level , and what the remove below does is to remove (0), so it can add (1).
return;
}
int max = target / coins[index];
for(int i = 0; i <= max; i++){
cur.add(i);
helper(target - i * coins[index], coins, index + 1, cur, result);
cur.remove(cur.size() - 1); // this removes the previous possible freq,
// for example, max = 3, i can be 0, 1, 2, after cur.add(0), we want cur.add(1).but
// since cur still has (0), we need to remove 0 before add 1
}
}
private List<Integer> another_one(int[] input, List<Integer> answer){
List<Integer> current = new ArrayList<>();
for(int i = 0; i < answer.size(); i++){
if(answer.get(i) != 0){
for(int j = 0; j < answer.get(i); j++){
current.add(input[i]);
}
}
}
return current;
}
}
标签:font convert number arraylist eve max 2.0 can new
原文地址:https://www.cnblogs.com/tobeabetterpig/p/9321613.html