import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/combination-sum/
*
* Created by lverpeng on 2017/7/14.
*
* 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]
*
*/
public class CombinationSum {
private List<List<Integer>> result = new ArrayList<List<Integer>>(); /**
* 先考虑一个数字,比如第一位,其他位以此类推,第一位为例
*
*
* @param num
* @param start
* @param end
* @param target
* @param list
*/
public void combinationSum (int[] num, int start, int end, int target, List<Integer> list) {
if (target == 0 && list.size() > 0) {
Integer[] newList = new Integer[list.size()];
System.arraycopy(list.toArray(new Integer[list.size()]), 0, newList, 0, list.size());
result.add(Arrays.asList(newList));
return ;
}
int index = start;
int multiple = 0;
while (index <= end && num[index] <= target) {
if (index > start && num[index] == num[index-1]) {
index ++;
continue;
}
multiple = target / num[index];
for (int i = 1; i <= multiple; i++) {
int newTarget = target - i * num[index];
list.add(num[index]);
combinationSum(num, index + 1, end, newTarget, list);
list.remove(list.size() - 1);
}
index ++; }
} public List<List<Integer>> combination (int[] num, int target) {
Arrays.sort(num);
List<Integer> combinationList = new ArrayList<Integer>();
combinationSum(num, 0, num.length - 1, target, combinationList);
return result;
} private static void printMatrix (List<List<Integer>> list) {
StringBuilder stringBuilder = new StringBuilder();
for (List<Integer> intList : list) {
for (Integer num : intList) {
stringBuilder.append(num);
stringBuilder.append(", ");
}
stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length() - 1);
stringBuilder.append("\n");
}
System.out.println(stringBuilder);
} public static void main(String[] args) {
CombinationSum combinationSum = new CombinationSum();
int[] num = new int[]{2,3,6,7};
printMatrix(combinationSum.combination(num, 7));
}
}

leetcode — combination-sum的更多相关文章

  1. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  5. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  6. LeetCode: Combination Sum I && II && III

    Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...

  7. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  8. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  9. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  10. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. Error resolving template [xxx], template might not exist or might not be exist

    Springboot+thymeleaf+mybatis 抛Error resolving template [xxx], template might not exist的异常 原因是我们在pom. ...

  2. 关于Asp.net事件,如何在触发子控件的事件时,同步触发父页面的事件

    对页面引用自定义控件后,通过绑定自定义事件,页面绑定子控件的事件,在子控件做了某些修改动作后,如何同步操作父页面的方法:下面我煮了个栗子,同学们可以来尝一尝试一试 a.aspx 引用 UserCont ...

  3. 利用python list 完成最简单的DB连接池

    先来看查看效果: 在代码连接数据库后,并且执行三条sql后,将mysql直接重启掉,故我们的连接池连接均是不ok的,所以,它会全部删除再抓新的连接下来,重启mysql命令: 关于python代码: # ...

  4. 简述Oracle IOT(Index Organized Table)

    转:http://blog.itpub.net/17203031/viewspace-744477 对关系型数据库产品(RDBMS)而言,一个重要特性就是:数据信息都被组织为二维数据表,信息的表达可以 ...

  5. 哈夫曼(Huffman)树和哈夫曼编码

    一.哈夫曼(Huffman)树和哈夫曼编码 1.哈夫曼树(Huffman)又称最优二叉树,是一类带权路径长度最短的树, 常用于信息检测. 定义: 结点间的路径长度:树中一个结点到另一个结点之间分支数目 ...

  6. Oracle 12c client with .NET legacy Oracle driver

    如果使用Oracle 12c Client和.NET的Oracle driver,你很可能会碰到跟下面一样的问题: https://www.codeproject.com/Questions/8767 ...

  7. HARD FAULT

    程序陷在while(1)里面 解决办法 定点到发生死循环的位置 打开stack windows逐层查找发生死循环之前运行过的函数 导致原因 1 内存溢出或者访问越界,通常为数组或结构体访问越界.这个需 ...

  8. 学习笔记----html的lang属性

    lang属性的取值应该遵循 BCP 47 - Tags for Identifying Languages. 单一的 zh 和 zh-CN 均属于废弃用法. 问题主要在于,zh 现在不是语言code了 ...

  9. java笔试之输出

    1. public class foo { private static void testMethod(){ System.out.println("testMethod"); ...

  10. 虚拟机找不到/mnt/hgfs挂载目录——debian与 vmware

    如果在安装好 VMware Tools 并在设置里面设定好共享目录之后仍然找不到 /mnt/hgfs 默认挂载目录,那么尝试以下步骤: 1. 确认VMware Tools 和共享目录设定已经完成: 2 ...