题解 | #加起来和为目标值的组合(四)#
加起来和为目标值的组合(四)
https://www.nowcoder.com/practice/7a64b6a6cf2e4e88a0a73af0a967a82b
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @param target int整型
* @return int整型
*/
// ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
int res = 0;
LinkedList<Integer> path = new LinkedList<Integer>();
public int combination (int[] nums, int target) {
getCombine(target, nums);
return res;
}
private void getCombine(int target, int[] nums) {
if (target < 0) {
return;
}
if (target == 0) {
res++;
return;
}
for (int i = 0; i < nums.length; ++i) {
path.add(nums[i]);
getCombine(target - nums[i], nums);
path.removeLast();
}
}
}
查看10道真题和解析