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 ...
随机推荐
- 谷歌浏览器运行Flash
最近有人问我谷歌浏览器的flash总是要点击手动运行才可以使用.看了很多网上很多教程,并没有比较好的解决方案. 自己找了相关资料后,找到了一个比较好的完整的.特此在这边放出来给大家使用. 新建记事本, ...
- 《Linux就该这么学》第十天课程
使用RAID与LVM磁盘阵列技术 有RAID 0,RAID 1,,RAID 5,RAID 1 0等,下面列举RAID的各个概况 1. RAID 0 RAID 0技术把多块物理硬盘设备(至少两块)通过硬 ...
- Linux---一级/二级目录以及位置目录名/指令
home目录:普通用户登录进来以后的初始位置(会在home目录下创建一个登录名相同的目录例如 / home / 用户名),如果是超级用户则就是 在根目录 /下的 root目录(也就是 /root) ...
- weblogic获取应用目录路径(war包)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcepti ...
- vim配置文件.vimrc
20171127备份 syntax on "自动语法高亮 set number "显示行号 set autoindent "回车后自动缩进 set tabstop=4 & ...
- 1.准备工作之Groovy
Groovy(读做:gu : ru : wei) Groovy是一种运行在jvm上的动态语言,它吸取了Python.Ruby和SmallTalk等语言的优点:在Java的基础之上增加了许多特色功能,相 ...
- NLP文本相似度
NLP文本相似度 相似度 相似度度量:计算个体间相似程度 相似度值越小,距离越大,相似度值越大,距离越小 最常用--余弦相似度: 一个向量空间中两个向量夹角的余弦值作为衡量两个个体之间差异的大小 余 ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- CentOS6.2上安装pip
如果yum install pip执行失败.按如下步骤执行 首先安装epel扩展源: yum -y install epel-release 然后执行yum --disablerepo=epel -y ...
- Asp.net Security框架(2)
Asp.net 的Security框架除了提供Cookies,OAuth,ActiveDirectory等多个用户认证实现,基本上已经满足业务项目的开发需要了. 当需要实现OAuth2.0服务器端实现 ...