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 number in C may only be used once in the combination.

这里数字不需要重复出现,所以使用dfs的时候,循环从上一次的下一个位置开始就可以了。

但是这样会出现重复的结果。比如[1,2,3,1],因为第一个位置可以选两次1,所以可能会出现重复结果。

所以这里排序是为了方便避免重复的结果。[1,1,2,3],当为第一个位置选择时,就不必要选两次1了。

 1 public static ArrayList<ArrayList<Integer>> combinationSum2(int[] candidates, int target) {
 2         Arrays.sort(candidates);
 3         ArrayList<Integer> tmp = new ArrayList<Integer>();
 4         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 5         process(candidates, 0, tmp, result, 0, target);
 6         return result;
 7     }
 8
 9     public static void process(int[] candidates, int start, ArrayList<Integer> tmp, ArrayList<ArrayList<Integer>> result,
10             int sum, int target) {
11         if (sum == target) {
12             result.add(new ArrayList<Integer>(tmp));
13             return;
14         }
15         else if (sum < target) {
16             for (int i=start; i<candidates.length; i++) {
17                 tmp.add(candidates[i]);
18                 process(candidates, i+1, tmp, result, sum+candidates[i], target);
19                 tmp.remove(tmp.size()-1);
20                 while(i<candidates.length-1 && candidates[i] == candidates[i+1]) i++;
21             }
22         }
23     }

转载于:https://www.cnblogs.com/longhorn/p/3537548.html

LeetCode: Combination Sum II相关推荐

  1. LeetCode Combination Sum IV(动态规划)

    问题:给出一个数组nums和目标数target,问有多少组合形式 思路:用dp(i)表示目标数target的组合数.则有状态转移关系为dp(i)=sum(dp(i-nums[j])),其中i>= ...

  2. 40. Combination Sum II 组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  3. Combination Sum 和Combination Sum II

    这两道题的基本思路和combination那一题是一致的,也是分治的方法. 其中combination Sum复杂一点,因为每个数可能用多次.仔细分析下,本质上也是一样的.原来是每个数仅两种可能.现在 ...

  4. C#LeetCode刷题之#40-组合总和 II(Combination Sum II)

    目录 问题 示例 分析 问题 该文章已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3666 访问. 给定一个数组 candidates ...

  5. 【DFS】LeetCode 40. Combination Sum II

    Solution1:我的答案 同39题.DFS时间复杂度O(2n)O(2n)O(2^n),空间复杂度O(kn)O(kn)O(kn),k是最终答案的数量,n是元素个数 去重,用set,比较偷懒的做法 c ...

  6. 递归/回溯:Combination Sum II数组之和

    问题如下: 已知一组数(其中有重复元素),求这组数可以组成的所有子集中,子 集中的各个元素和为整数target的子集,结果中无重复的子集. 例如: nums[] = [10, 1, 2, 7, 6, ...

  7. LeetCode Combination Sum

    因为实验室项目好久没刷题了.从今天开始重新开始刷题. Given a set of candidate numbers (C) and a target number (T), find all un ...

  8. 回溯法和DFS leetcode Combination Sum

    代码: 个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解.而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法 ...

  9. LeetCode Path Sum II(dfs或者bfs)

    问题:给出一个树和一个数,求出从根结点到叶子结点路径和等于这个数的所有情况 思路: 1.深度优先搜索,在到达一个深度结点时,判断是否是叶子结点,并且判断和是否等于要求的数.如果满足,说明是满足条件的一 ...

  10. functionclass[LeetCode]Path Sum II

    在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获.只有追求 ...

最新文章

  1. sizeof 操作符详解
  2. 高能预警!各路大神正火速奔赴8月WOT2016 移动互联网技术峰会
  3. nodejs报错解决:Error: Can only perform operation while paused. - undefined
  4. python怎么导入文本-Python 导入文件问题
  5. 7限制cpu使用_Kubernetes 资源配额使用指南 | Linux 中国
  6. 文件流、目录流、文件描述符总结
  7. java 像素级碰撞检测,» 像素级碰撞检测类
  8. 学习SQL:INNER JOIN与LEFT JOIN
  9. 大学计算机与应用软件,深圳大学
  10. 计算机解决最小二乘法的过程,线性最小二乘法在计算机化学中的应用.doc
  11. Transaction使用及原理
  12. Elasticsearch顶尖高手系列:高手进阶篇(一)
  13. 高效实用GitHub关键字搜索~~干货干货~~
  14. 计算机数学基础知识点归纳,计算机数学基础--详细介绍
  15. Mac系统下Gauge初体验
  16. js map基本操作和循环取值
  17. C# 实现刻录光盘功能
  18. 史无前例的全球疫苗大接种 | 经济学人全球早报精选
  19. 技术赋能水务数字化转型,上海市水旱灾害防御技术中心领导一行调研上海控安
  20. Unity学习之预制件变体

热门文章

  1. Python输出异常信息(行号)
  2. 结构体中的map尽量使用指针,否则不要使用memset
  3. 多屏系统上播放幻灯片的设想
  4. 电信猫不折旧,用了几年还是原价
  5. NGINX 4xx 5xx 状态码构造
  6. iptables基本用法和linux网络相关
  7. 自动化运维之saltstack
  8. Vue项目中使用eslint的笔录,编辑器采用sublime3
  9. 从难民到 Uber 首席技术官:一个亚裔幸存者的故事
  10. 《Programming WPF》翻译 第8章 3.Storyboard