39. Combination Sum
来源:互联网 发布:nat123免费域名 编辑:程序博客网 时间:2024/06/03 08:53
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) 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 target7
,
A solution set is:[ [7], [2, 2, 3]]
题目大意:给定一个集合,和一个目标数字,找出集合中所有的数,使其和为给出的目标数字。每个数字可以被使用多次。
思路:回溯。
代码:
package Array;import java.util.ArrayList;import java.util.List;/** * @Author OovEver * @Date 2017/11/28 10:10 */public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> res = new ArrayList<>(); backtrack(res, new ArrayList<>(), candidates, target, 0); return res; } private void backtrack(List<List<Integer>> res, List<Integer> tempList, int[] nums, int target, int start) { if (target < 0) { return; } else if (target == 0) { res.add(new ArrayList<>(tempList)); } else { for(int i=start;i<nums.length;i++) { tempList.add(nums[i]); backtrack(res, tempList, nums, target - nums[i], i); tempList.remove(tempList.size() - 1); } } }}
阅读全文
0 0
- 39. Combination Sum && 40. Combination Sum II
- 39. Combination Sum &&40. Combination Sum II
- [LeetCode]39.Combination Sum
- LeetCode --- 39. Combination Sum
- LeetCode 39.Combination Sum
- [Leetcode] 39. Combination Sum
- 39. Combination Sum
- 39. Combination Sum
- Leetcode-39.Combination Sum
- LeetCode 39. Combination Sum
- [LeetCode]39. Combination Sum
- 39. Combination Sum LeetCode
- [leetcode] 39. Combination Sum
- 39. Combination Sum
- 39. Combination Sum
- leetcode 39. Combination Sum
- 【leetcode】 39. Combination Sum
- 39. Combination Sum(backtracking)
- 教你用300万共享单车出行数据,预测骑行目的地 !(附源码)
- [DL]基于pytorch的Elman RNN语言模型
- [RK3399][Android7.1] Ubuntu 编译环境之Jdk版本切换
- gunicorn 部署 flask 应用
- Hive2.0函数大全(中文版)
- 39. Combination Sum
- flask转换器
- 最长上升子序列(LIS)和最长下降子序列(LDS)
- 抽象类(abstract class)和接口(interface)有什么异同?
- linux 下 du, df, top, free, pstack, su, sudo(sudo -, sudo -s), adduser, password命令介绍
- HashMap基础的学习
- 线性代数与高等代数——学习资料(更新..........)
- OpenStack公共组件oslo之十二——oslo.policy
- 使用anaconda搭建TensorFlow环境