39. Combination Sum

布满荆棘的人生 2021-12-22 06:49 383阅读 0赞
  1. public class Solution {
  2. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  3. List<List<Integer>> ret=new ArrayList<List<Integer>>();
  4. Arrays.sort(candidates);
  5. generateCombination(new ArrayList<Integer>(), 0, ret, candidates, target);
  6. return ret;
  7. }
  8. private void generateCombination(List<Integer> list, int idx, List<List<Integer>> combs, int[] candidates, int target){
  9. if(target==0)
  10. {
  11. combs.add(new ArrayList<Integer>(list));
  12. return;
  13. }
  14. if(target<0||idx>=candidates.length)
  15. return;
  16. generateCombination(list, idx+1, combs, candidates, target);
  17. list.add(candidates[idx]);
  18. generateCombination(list, idx, combs, candidates, target-candidates[idx]);
  19. list.remove(list.size()-1);
  20. }
  21. }

转载于:https://www.cnblogs.com/asuran/p/7584309.html

发表评论

表情:
评论列表 (有 0 条评论,383人围观)

还没有评论,来说两句吧...

相关阅读