Leetcode Combination Sum
来源:互联网 发布:centos安装nginx 编辑:程序博客网 时间:2024/05/22 16:58
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]]
Difficulty: Medium
Solution: BackTracking
public class Solution { public void helper(int[] nums, int index, int target, List<List<Integer>> res, List<Integer> temp){ if(index >= nums.length || nums[index] > target) return; for(int i = index; i < nums.length; i++){ if(nums[i] == target){ temp.add(nums[i]); res.add(new ArrayList<Integer>(temp)); temp.remove(temp.size() - 1); return; } else if(nums[i] < target){ temp.add(nums[i]); helper(nums, i, target - nums[i], res, temp); temp.remove(temp.size() - 1); } else{ return; } } } public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<List<Integer>>(); Arrays.sort(candidates); List<Integer> temp = new ArrayList<Integer>(); helper(candidates, 0, target, res, temp); return res; }}
0 0
- [LeetCode] Combination Sum、Combination Sum II
- 【LeetCode】Combination Sum && Combination Sum II
- leetcode之Combination Sum && Combination Sum II
- leetcode Combination Sum &Combination Sum II
- leetcode-combination sum and combination sum II
- Leetcode:Combination Sum与Combination Sum II
- leetcode Combination Sum VS Combination Sum II
- [Leetcode]Combination Sum &&Combination Sum II
- LeetCode Combination Sum & Combination Sum II
- leetcode | Combination Sum & Combination Sum 2
- LeetCode: Combination Sum
- LeetCode: Combination Sum II
- LeetCode: Combination Sum
- LeetCode: Combination Sum II
- [LeetCode]Combination Sum
- [LeetCode]Combination Sum II
- LeetCode Combination Sum
- LeetCode Combination Sum II
- <Service> 简介
- KOA学习(1)——介绍,安装与运行
- 算法之美--选择排序--直接选择排序
- CentOS 6.5 安装Clang 3.5.0
- 【node.js】本地模式安装express:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件。
- Leetcode Combination Sum
- mysql linux下源码安装错误集锦
- 如何重装win7
- XML解析之DOM解析一层解析1
- TCP首部
- 线程(六)线程池
- 装饰模式在iOS开发中的应用
- CocoaPods升级后(最新版本1.0.1),安装第三方库的时候会报如下错误:
- Struts2实现文件上传下载