[LeetCode]--40. Combination Sum II
来源:互联网 发布:mac 卸载搜狗输入法 编辑:程序博客网 时间:2024/06/05 12:38
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.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]
跟上一篇思想其实是一样的,只不过这个不能用同一水平线上即不能重复一个元素。上代码,递归自己写出来,看来还是懂了,嘿嘿。
public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if (candidates.length == 0) return res; List<Integer> list = new ArrayList<Integer>(); Arrays.sort(candidates); findSum(candidates, list, 0, 0, target, res); return res; } private void findSum(int[] candidates, List<Integer> list, int sum, int level, int target, List<List<Integer>> res) { if (sum == target) { if (!res.contains(list)) res.add(new ArrayList<Integer>(list)); return; } else if (sum > target) return; else for (int i = level; i < candidates.length; i++) { list.add(candidates[i]); findSum(candidates, list, sum + candidates[i], i+1, target, res); list.remove(list.size() - 1); } }}
0 0
- [LeetCode]40.Combination Sum II
- LeetCode --- 40. Combination Sum II
- LeetCode 40.Combination Sum II
- [Leetcode] 40. Combination Sum II
- LeetCode 40. Combination Sum II
- [leetcode] 40. Combination Sum II
- 40. Combination Sum II LeetCode
- leetcode 40. Combination Sum II
- Leetcode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- leetcode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- LeetCode 40. Combination Sum II
- LeetCode *** 40. Combination Sum II
- 【LeetCode】40. Combination Sum II
- leetcode 40. Combination Sum II
- LeetCode - 40. Combination Sum II
- [LeetCode] 40. Combination Sum II
- com.sun.proxy.$Proxy4 cannot be cast to spring.HelloService
- EXCEL:取消合并且等于原合并值
- s3c2440 安装工具链出现No such file or directory
- select,poll,epoll差别
- Android View的事件分发机制
- [LeetCode]--40. Combination Sum II
- 判断素数
- Docker容器内多进程管理(一)-Supervisor
- 垃圾回收算法之标记-整理算法
- 选票统计
- unity shader中的矩阵变换知识
- 判断闰年
- JS日期选择器(兼容IE,Firefox,Opera等主流浏览器)
- linux 安装Qt