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 ...
随机推荐
- tensorflow学习之(九)classification 分类问题之分类手写数字0-9
#classification 分类问题 #例子 分类手写数字0-9 import tensorflow as tf from tensorflow.examples.tutorials.mnist ...
- Python Day 7
阅读目录 内容回顾: 数据类型相互转换: 字符编码: ##内容回顾 #1.深浅拷贝 ls = [1, 'a', [10]] 值拷贝:直接赋值 ls1 = ls, ls中的任何值发生改变,ls1中的值都 ...
- go的基本数据类型
一,数据类型的介绍 在go语言中,数据类型是用于声明函数和变量的:数据类型是为了把数据分成所需内存不同大小的数据,除了在需要使用大数据的时候才会申请大内存,这样就会充分的使用内存 Go 语言按类别有以 ...
- ScrollView无法滚动
ScrollView视图无法滚动 箭头所指地方应该用dp单位的数值
- 第49章:MongoDB-数据导入导出
①导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导出 ...
- spring aop 切面编程中获取具体方法的方法
spring 切面编程中获取具体方法的方法 工作中,使用环绕通知,用来捕获异常,然后通过获取方法的返回值,返回不同的数据给到调用方. 由于方法的返回值不同,我们处理异常时,也需要返回不同的格式. 这时 ...
- 学以致用三十三-----django生命周期
听了讲解django的视频后,受益匪浅,每个知识点老师都会总结整理.学会总结,存为己用. django生命周期弄明白后,对于整个django的框架会有一个清晰的了解.先上图 1.客户端发送请求,在dj ...
- android踩坑日记1
Android四大组件-活动.服务.广播.碎片 情况一 应用场景:定时从服务器获取数据,然后活动或者碎片中根据最新获得的数据,更新UI. 思考: 首先定时,想到定时器,推荐使用系统自带的AlertMa ...
- getElementById和$()获取值一点注意事项
<script type="text/javascript"> window.onload = function () { var obj = document.get ...
- JAVA课程设计---学生基本信息管理系统
1.团队课程设计博客链接 http://www.cnblogs.com/zyjjj/p/7061880.html 2.个人负责模块或任务说明 函数 功能说明 Search 查找学生信息,分为两种查找方 ...