Combination Sum —— LeetCode
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.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- 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]
题目大意:给一个候选数组,一个目标值,从候选数组找出所有和等于目标值的List,候选数组元素可以重复。
解题思路:还是用回溯的方式来做,因为可以重复,所以每次都可以从当前元素开始往后依次添加到List,递归判断,然后回溯。
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if (candidates == null || candidates.length == 0) {
return res;
}
List<Integer> tmp = new ArrayList<>();
helper(res, tmp, 0,candidates, target);
return res;
} private void helper(List<List<Integer>> res, List<Integer> tmp, int start,int[] candidates, int target) {
if (0 == target) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i < candidates.length && target >= candidates[i]; i++) {
tmp.add(candidates[i]);
helper(res, tmp, i,candidates, target - candidates[i]);
tmp.remove(tmp.size() - 1);
int ca = candidates[i];
while(i<candidates.length&&ca==candidates[i]){
i++;
}
}
}
Combination Sum —— LeetCode的更多相关文章
- 39. Combination Sum - LeetCode
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...
- Combination Sum [LeetCode]
Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicat ...
- Combination Sum leetcode java
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Backtracking Template for (Subsets, Permutations, and Combination Sum)
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- codevs 3052 多米诺 二分图匹配
/*codevs 3052 二分图匹配 把矩阵分两批 黑和白 且黑白不相交 这就构成了二分图的两部分 然后求最大匹配*/ #include<cstdio> #include<cstr ...
- sql知识点的积累和使用过的例子
越来越发现自己的sql方面的知识的欠缺,所以只能放低姿态一点一点的学了 一 游标和charIndex的使用. 游标我一直没用过,以前只是在同事们写的存储过程里见过,但是一直没看明白(可是我就是比较笨吧 ...
- C++ 简单的入门语法
入门的hello world using namespace std; 是使用命名空间,有点像java里面的引入包main 方法和java一样是主入口,有且只有一个,因为是int ,所以还必须返回一个 ...
- 解决右滑返回手势和UIScrollView中的手势冲突
当在一个viewController中添加了scrollView或者tableView的时候,贴边侧滑返回的时候会首先触发滚动而失效,要解决这个问题,需要通过requireGestureRecogni ...
- 一种实现C++反射功能的想法(二)
在介绍我的思路前, 让我们准备下预备知识 C++是怎么实现类函数的绑定的. 我们知道类的非静态成员函数是存储在全局区, 并在内存中只保存一份副本. 我们调用非静态成员函数是通过类对象进行调用. 那么如 ...
- SGU 179.Brackets light
时间限制:0.25s 空间限制:12M 题意 给定一个合法的仅由'(',')'组成的括号序列,求它的下一个合法排列.假定'('<')'. Solution: ...
- 『重构--改善既有代码的设计』读书笔记----Replace Data Value with Object
当你在一个类中使用字段的时候,发现这个字段必须要和其他数据或者行为一起使用才有意义.你就应该考虑把这个数据项改成对象.在开发初期,我们对于新类中的字段往往会采取简单的基本类型形式来保存,但随着我们开发 ...
- spring Mvc json返回json的日期格式问题
(一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...
- splice从数组中删除指定定数据
/*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...
- ubuntu 安装 open in teminal
sudo apt-get install nautilus-open-terminalnautilus -q