leetcode — combination-sum
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的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
		Given an integer array with all positive numbers and no duplicates, find the number of possible comb ... 
- [LeetCode] Combination Sum III 组合之和之三
		Find all possible combinations of k numbers that add up to a number n, given that only numbers from ... 
- [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 ... 
- LeetCode Combination Sum III
		原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ... 
- LeetCode: Combination Sum I && II && III
		Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ... 
- LeetCode: Combination Sum 解题报告
		Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ... 
- [Leetcode] Combination Sum 系列
		Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ... 
- LeetCode:Combination Sum I II
		Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ... 
- LeetCode: Combination Sum II 解题报告
		Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ... 
随机推荐
- C++数论板题(弹药科技):Lengendre定理和欧拉函数
			弹药科技 时间限制: 1 Sec 内存限制: 128 MB 题目描述 经过精灵族全力抵挡,精灵终于坚持到了联络系统的重建,于是精灵向人类求助, 大魔法师伊扎洛决定弓}用博士的最新科技来抗敌. 伊扎洛: ... 
- Java平台
			Java的平台无关性 不同的网络环境,操作系统 支持嵌入式设备 减少开发部署时间 Java自身的平台和语言 编译成class文件 可在Java虚拟机中运行,与外部环境无关(对虚拟机的依赖) 通过外部A ... 
- DAO和service的解释
			转自:http://blog.sina.com.cn/s/blog_4b1452dd0102wvox.html 我们都知道有了Hibernate后,单独对数据的POJO封装以及XML文件要耗损掉一个类 ... 
- Anfora 自动装载类
			1.目录结构 + Anfora | + Autoload | - ClassLoader.php | - Autoload.php - autoload.php 2.注册类加载器 src/Anfora ... 
- C#框架类
			C# 开源框架(整理) Json.NET http://json.codeplex.com/ Json.Net 是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用J ... 
- vue组件自定义属性命名
			今天自己写vue组件demo的时候发现一个有趣的问题:vue组件自定义属性命名不支持用驼峰命名! 上面图示为正常情况下的自定义属性,没有任何问题. 但是一旦出现自定义属性中包含了大写字母,则如下图所示 ... 
- java(二)Web部分
			2.1.1讲一下http get和post请求的区别? GET和POST请求都是http的请求方式,用户通过不同的http的请求方式完成对资源(url)的不同操作.GET,POST,PUT,DELET ... 
- C#关于xml文件和TreeView之间的转换解析
			主窗体: using System; using System.Collections; using System.Collections.Generic; using System.Componen ... 
- iOS 数据归档----温故而知新
			#import "StudyViewController.h" #import "person.h" @interface StudyViewControlle ... 
- c# 字符串中多个连续空格转为一个空格
			#region 字符串中多个连续空格转为一个空格 /// <summary> /// 字符串中多个连续空格转为一个空格 /// </summary> /// <param ... 
