1. 原题链接

https://leetcode.com/problems/combination-sum/description/

2. 题目要求

给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合

注意:(1)数组中的每一个元素可以重复用;(2)数组中不存在重复元素;(3)数组中都是正整数

3. 解题思路

采用迭代的方法检验所有元素组合

4. 代码实现

 import java.util.ArrayList;
import java.util.List; public class CombinationSum39 {
public static void main(String[] args) {
CombinationSum39 cs = new CombinationSum39();
int[] candidates = {,,,};
for (List l1:cs.combinationSum(candidates,)){
System.out.println(l1.toString());
System.out.println();
} }
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
combinationSum(result,new ArrayList<Integer>(),candidates,target,);
return result;
}
public void combinationSum(List<List<Integer>> result, List<Integer> cur, int[] candidates, int target,int start) {
if (target > ) {
for (int i = start;i < candidates.length;i++) {
cur.add(candidates[i]);
combinationSum(result, cur, candidates, target-candidates[i],i);
cur.remove(cur.size() - );
}
}
if (target == )
result.add(new ArrayList<Integer>(cur));
}
}

LeetCode:39. Combination Sum(Medium)的更多相关文章

  1. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  2. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

  3. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  4. LeetCode:49. Group Anagrams(Medium)

    1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...

  5. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  6. LeetCode:9. Palindromic Number(Medium)

    原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...

  7. LeetCode题解39.Combination Sum

    39. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T ...

  8. [Leetcode][Python]39: Combination Sum

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 39: Combination Sumhttps://oj.leetcode. ...

  9. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

随机推荐

  1. (转)浅谈PostgreSQL的索引

    1. 索引的特性 1.1 加快条件的检索的特性 当表数据量越来越大时查询速度会下降,在表的条件字段上使用索引,快速定位到可能满足条件的记录,不需要遍历所有记录. create table t(id i ...

  2. MATLAB基础指令操作

    由于课程实验需要学习使用了MATLAB,在此记录一下MATLAB的基本操作和命令,供参考与查阅. 学习过程中的资料也链接如下: MATLAB矩阵运算:https://wenku.baidu.com/v ...

  3. thuwc2018 爆炸记

    从没考过这么差,必须好好总结一下.. $day1$: 上午到了雅礼洋湖,下午就开始考试.. 食堂饭菜还是很不错的,听说都是雅礼自己垫的? 下午的$day1$爆炸了.. 开考以后看了一下三个题,感觉一开 ...

  4. centos7 firewall指定IP与端口访问(常用)

    1.启动防火墙 systemctl start firewalld.service 2.指定IP与端口 firewall-cmd --permanent --add-rich-rule="r ...

  5. Kali-linux安装并配置NVIDIA显卡驱动

    显卡驱动程序就是用来驱动显卡的程序,它是硬件所对应的软件.驱动程序即添加到操作系统中的一小块代码,其中包含有关硬件设备的信息.有了此信息,计算机就可以与设备进行通信.驱动程序是硬件厂商根据操作系统编写 ...

  6. SSM框架构建多模块之业务拆分实践

    在如下这两篇篇文章我都或多或少强调过业务分层方面的的方法和注意事项,感兴趣的可以看看: 系统设计和系统划分有定律可循 业务拆分的思考 之前是说,现在是做.以我个人博客为例,我的博客最初只是一个单体应用 ...

  7. 日期插件kalendae,遇到的一些问题

    1.日期中文显示 /*_months : 'January_February_March_April_May_June_July_August_September_October_November_D ...

  8. MySQL常用API函数

    -- 返回数字的绝对值 SELECT ABS(-10) -- 返回不大于X的最大整数值 SELECT FLOOR(10.9) -- 返回不小于X的最大整数值 SELECT CEILING(10.4) ...

  9. javaScript 工作必知(十一) 数组常用方法实现

    大纲 Array join reverse反转 sort排序 concat 拼接 slice splice 数组 //定义数组 var a = []; //使用Array定义一个数组, var a1 ...

  10. File常见操作函数

    String Name = File.getName();  //获得文件或文件夹的名称: String parentPath = File.getParent();  //获得文件或文件夹的父目录 ...