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. MUI中超链接失效解决办法

    重新绑定a标签点击事件,用 plus.runtime.openURL(this.href) 打开新页面

  2. 87、代码适配IphoneX

    一.APP在iphoneX运行后不能占满,上下都有多余的边 解决方法:把旧的image.xcassets中的LaunchImage删掉,重新创建并在Images.xcassets中为iPhone X添 ...

  3. js 控制光标到指定位置

    js控制光标到指定节点位置(适用于富文本编辑器中) function placeCaretAtEnd(el) { //传入光标要去的jq节点对象 el.focus(); if (typeof wind ...

  4. web端代码提示

    web端代码提示 这个功能是基本完成了,但是与需求不一致.但是废弃挺可惜的,所以就单独拿出来作为一个例子记录一下. 其中还包括了,java代码的自动编译和执行,在web端显示执行结果. 下载链接: h ...

  5. Linux 第十四天

    6)Bash常用快捷键 快捷键 作用 ctr1+ a 把光标移动到命令行开头.如果我们输入的命令过长,想要把光标移| 动到命令行开头时使用. ctr1+e 把光标移动到命令行结尾. ctr1+c 强制 ...

  6. pychrom 中文版

    http://jingyan.baidu.com/article/a378c960daf80eb328283033.html

  7. Maven学习笔记5:Maven属性、profile和资源过滤

    Maven的六类属性 内置属性 主要有两个常用内置属性:${basedir}项目的根目录(包含pom.xml文件的目录),${version}项目版本 POM属性 用户可以使用该属性引用POM文件中对 ...

  8. NotePad++ 添加HEX-Editor插件

    步骤: 一.下载插件 https://github.com/chcg/NPP_HexEdit/releases 二.选择插件版本 Notepad 官网客服提示:32bit Notepad++可以使用常 ...

  9. Linux-3.0.8中基于S5PV210的IRQ模块代码追踪和分析

    init/main.c: asmlinkage void start_kernel(void) { ...... early_irq_init(); init_IRQ(); ...... } earl ...

  10. ICO图标下载地址

    http://findicons.com/ http://www.iconfont.cn/