Combination Sum_Leetcode_#39
来源:互联网 发布:软件安全检测平台 编辑:程序博客网 时间:2024/06/10 23:56
1 题目
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]
]
2 解法
public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { Arrays.sort(candidates); List<List<Integer>> result = new ArrayList<List<Integer>>(); getResult(result, new ArrayList<Integer>(), candidates, target, 0); return result; } public void getResult(List<List<Integer>> result, List<Integer> cur, int[] candidates, int target, int start){ if(target > 0){ for(int i = start; i < candidates.length && target >= candidates[start]; i++){ cur.add(candidates[i]); getResult(result, cur, candidates, target - candidates[i], i); cur.remove(cur.size() - 1); } }else if(target == 0){ result.add(new ArrayList<Integer>(cur)); } }}
0 0
- Combination Sum_Leetcode_#39
- Two Sum_leetcode_#1
- 3Sum_Leetcode_#15
- 4Sum_Leetcode_#18
- LeetCode 39: Combination Sum
- LeetCode(39) Combination Sum
- [leetcode 39] Combination Sum
- leetcode 39: Combination Sum
- leetcode || 39、Combination Sum
- 39-m-Combination Sum
- leetcode 39 : Combination Sum
- leetcode 39: Combination Sum
- Leetcode #39 Combination Sum
- LeetCode(39) Combination Sum
- LeetCode-39 Combination Sum
- 39 Combination Sum
- 39Combination Sum
- leetcode 39:Combination Sum
- POJ 1703 Find them, Catch them
- 系统定时任务
- HTML——表单
- Java开发中的23种设计模式详解
- hdu 5874 构造
- Combination Sum_Leetcode_#39
- Hive 2.1 安装
- 进程通信--管道
- HDU2647-Reward
- Cookie和Session会话管理基础
- 树和二叉树
- Combination Sum II_Leetcode_#40
- effective stl 第18条: 避免使用vector<bool>
- hdu5869——Different GCD Subarray Query(思考+树状数组)