关闭

leetcode-java-40. Combination Sum II

标签: leetcodejava
188人阅读 评论(0) 收藏 举报
分类:
/*
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 number may be chosen from C unlimited number of times.
Note:
•All numbers (including target) will be positive integers.
•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]
]
 */
/*
思路l:
深度优先搜索即可。
这个用递归来实现,递归终止条件:
1.如果sum>target直接返回
2.如果sum=target则保存结果
但需要注意的:
每次往result存储时,需要new ArrayList<Integer>,以免替换了以前的值
 */
 public class Solution {
     public List<List<Integer>> result;
     public List<Integer> seq;

     public List<List<Integer>> combinationSum(int[] candidates, int target) {
         result  = new ArrayList<List<Integer>>();
         seq = new ArrayList<Integer>();

         Arrays.sort(candidates);
         findPaths(candidates,target,0,0);

         return result;
     }

     public void findPaths(int[] candidates,int target,int sum,int level) {
         if(target == sum) {
             result.add(new ArrayList<Integer> (seq));
             return;
         }
         if(target < sum) {
             return;
         }

         for(int i = level;i < candidates.length;i++) {
            //  相当于是当前节点压入栈
             seq.add(candidates[i]);
             sum += candidates[i];
             findPaths(candidates,target,sum,i);
            //  没返回值,也就是当前节点不符合,则弹出栈的意思
             seq.remove(seq.size() - 1);
             sum -= candidates[i];
         }
     }
 }
0
0
查看评论

[leetcode]40. Combination Sum II ,python实现【medium难度】

Combination sum 2.python实现
  • zl87758539
  • zl87758539
  • 2016-06-16 17:37
  • 710

[LeetCode 39&40] Combination Sum I & II

题目链接:combination-sum import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Given a set of candidate numbers (C) ...
  • ever223
  • ever223
  • 2015-03-28 23:41
  • 826

【LeetCode】Combination Sum I & II 解题报告

【Combination Sum I】 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate n...
  • ljiabin
  • ljiabin
  • 2014-12-16 11:06
  • 6472

Combination Sum II -- LeetCode

原题链接: http://oj.leetcode.com/problems/combination-sum-ii/  这道题跟Combination Sum非常相似,不了解的朋友可以先看看,唯一的区别就是这个题目中单个元素用过就不可以重复使用了。乍一看好像区别比较大,但是其实实...
  • linhuanmars
  • linhuanmars
  • 2014-03-13 04:30
  • 15556

40. Combination Sum II(Python3)

40. Combination Sum II(Python3) 题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where th...
  • grape875499765
  • grape875499765
  • 2018-01-26 18:45
  • 33

[leetcode]39. Combination Sum,python实现【Medium难度】

39.COMBINATION SUM,PYTHON实现
  • zl87758539
  • zl87758539
  • 2016-06-16 17:01
  • 1459

leetcode【39+40+216+377 Combination Sum 相关】【python】

39Combination Sum是说让我们在给定数组C中找到所有的组合,使得组合中数字的和是target值。并且组合里的数字可以重复,也就是不限制C中每一个数字的使用。 这明显是回溯啦,大家记得让你判断有多少种呢就是动态规划,一旦让我们写出全部的组合那就是回溯。 回溯就是一直加进去数,满足条件...
  • u014251967
  • u014251967
  • 2016-12-13 16:38
  • 344

LeetCode40:Combination Sum II

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. Eac...
  • u012501459
  • u012501459
  • 2015-07-06 21:17
  • 794

【LeetCode with Python】 Combination Sum II

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 nu...
  • nerv3x3
  • nerv3x3
  • 2014-09-21 17:33
  • 2430

LeetCode --- 40. Combination Sum II

题目链接:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate nu...
  • makuiyu
  • makuiyu
  • 2015-02-06 21:39
  • 575
    个人资料
    • 访问:115221次
    • 积分:3045
    • 等级:
    • 排名:第13607名
    • 原创:198篇
    • 转载:4篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论