不知道写了多久。TestCase自己有问题:Combination Sum
2014-05-02 17:13
190 查看
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:
public class Solution {
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
Set<ArrayList<Integer>> s = new HashSet<ArrayList<Integer>>();
addResult(s, candidates, target, null);
result.addAll(s);
return result;
}
private void addResult(Set<ArrayList<Integer>> result, int[] candidate, int target, ArrayList<Integer> extras) {
for (int i = 0; i < candidate.length && candidate[i] <= target; i++) {
if (candidate[i] == target) {
ArrayList<Integer> is = new ArrayList<Integer>();
is.add(candidate[i]);
if (extras != null && extras.isEmpty() == false) {
is.addAll(extras);
Collections.sort(is);
}
result.add(is);
} else if (candidate[i] < target) {
ArrayList<Integer> extras2 = null;
if (extras == null) {
extras2 = new ArrayList<Integer>();
} else {
extras2 = (ArrayList<Integer>) extras.clone();
}
extras2.add(candidate[i]);
addResult(result, candidate, target - candidate[i], extras2);
}
}
}
}
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]
public class Solution {
public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
Set<ArrayList<Integer>> s = new HashSet<ArrayList<Integer>>();
addResult(s, candidates, target, null);
result.addAll(s);
return result;
}
private void addResult(Set<ArrayList<Integer>> result, int[] candidate, int target, ArrayList<Integer> extras) {
for (int i = 0; i < candidate.length && candidate[i] <= target; i++) {
if (candidate[i] == target) {
ArrayList<Integer> is = new ArrayList<Integer>();
is.add(candidate[i]);
if (extras != null && extras.isEmpty() == false) {
is.addAll(extras);
Collections.sort(is);
}
result.add(is);
} else if (candidate[i] < target) {
ArrayList<Integer> extras2 = null;
if (extras == null) {
extras2 = new ArrayList<Integer>();
} else {
extras2 = (ArrayList<Integer>) extras.clone();
}
extras2.add(candidate[i]);
addResult(result, candidate, target - candidate[i], extras2);
}
}
}
}
相关文章推荐
- 做了个超级简单的网页,不过不知道哪里有问题
- 大学毕业刚转行做计算机,毫无头绪,不知道怎么规划自己的技术?
- (C/C++面试题)没看过不知道自己有多差!!!!原本以为自己C还可以,也做过几个项目了,看了这些面试问题,我傻住了!!!!
- 上网的时候不知道自己电脑的IP地址怎么办? 找藏拙IP搜索
- logo丑不丑,品牌自己不知道吗?
- 自己做的一种计算器算法的设计..不知道代码是否有bug
- 客户真不知道自己想要什么
- 互联网中年危机,只是因为你不知道自己在什么行业
- 乱图乱画(不知道自己写的什么鬼)
- 不狠狠逼一下自己,永远不知道自己有多么强大
- 搜索引擎找得到你不知道自己公开的秘密
- 5 年后你希望自己是什么?你不知道,上帝也不知道
- 二叉树的遍历 不知道哪里有问题 求指点
- 不知道是否自己对她重要的不
- 自己写的strcamp函数不知道哪里错了
- 看别人的C/C++代码时发现自己所不知道的语法~
- 关于iTunes随机播放和我所不知道的自己
- 不知道自己做什么么
- 不知道在哪看的,感觉有点像自己的体会,自己修改了一点发出来
- 第一次有自己的BLog,还不知道怎么定义css呢!