LeetCode_39---Combination Sum
2015-06-18 11:42
483 查看
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
target
A solution set is:
Hide Tags
Array Backtracking
翻译:
Code:
package From21;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author MohnSnow
* @time 2015年6月18日 上午9:44:22
* @result 递归方法和动态规划方法
*/
public class LeetCode39 {
/**
* @param argsmengdx
* -fnst
*/
//backtracking---326msA---http://www.1point3acres.com/bbs/thread-108008-1-1.html
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates); // sort the array
recurse(new ArrayList<Integer>(), target, candidates, 0, result);
return result;
}
public static void recurse(List<Integer> list, int target, int[] candidates, int index, List<List<Integer>> result) {
if (target == 0) {
result.add(list);
return;
}
for (int i = index; i < candidates.length; i++) {
int newTarget = target - candidates[i]; // subtract candidate from target
if (newTarget >= 0) {
List<Integer> copy = new ArrayList<Integer>(list); // create a copy,include list
copy.add(candidates[i]);
recurse(copy, newTarget, candidates, i, result); // see if there is more
} else { // run out of target
break;
}
}
}
public static void main(String[] args) {
int[] candidates = { 1, 2, 3, 4, 6, 7, 9 };
int target = 7;
System.out.println("求和 : " + combinationSum(candidates, target).toString());
}
}
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,7and
target
7,
A solution set is:
[7]
[2, 2, 3]
Hide Tags
Array Backtracking
翻译:
Code:
package From21;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author MohnSnow
* @time 2015年6月18日 上午9:44:22
* @result 递归方法和动态规划方法
*/
public class LeetCode39 {
/**
* @param argsmengdx
* -fnst
*/
//backtracking---326msA---http://www.1point3acres.com/bbs/thread-108008-1-1.html
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Arrays.sort(candidates); // sort the array
recurse(new ArrayList<Integer>(), target, candidates, 0, result);
return result;
}
public static void recurse(List<Integer> list, int target, int[] candidates, int index, List<List<Integer>> result) {
if (target == 0) {
result.add(list);
return;
}
for (int i = index; i < candidates.length; i++) {
int newTarget = target - candidates[i]; // subtract candidate from target
if (newTarget >= 0) {
List<Integer> copy = new ArrayList<Integer>(list); // create a copy,include list
copy.add(candidates[i]);
recurse(copy, newTarget, candidates, i, result); // see if there is more
} else { // run out of target
break;
}
}
}
public static void main(String[] args) {
int[] candidates = { 1, 2, 3, 4, 6, 7, 9 };
int target = 7;
System.out.println("求和 : " + combinationSum(candidates, target).toString());
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统