您的位置:首页 > 编程语言 > Go语言

Algorithms—39.Combination Sum

2015-09-07 12:44 375 查看
思路:动态规划。

public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
return f(candidates, target, new HashMap<Integer, List<List<Integer>>>());
}
public List<List<Integer>> f(int[] candidates, int target,HashMap<Integer, List<List<Integer>>> map){
if (map.get(target)==null) {
List<List<Integer>> ans=new ArrayList<List<Integer>>();
if (target<candidates[0]) {
return ans;
}
for (int i = 0; i < candidates.length; i++) {
int k=candidates[i];
if (k<target) {
List<List<Integer>> list=f(candidates,target-k,map);
if (list!=null&&list.size()!=0) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).get(0)>=k) {
List<Integer> l=new ArrayList<Integer>();
l.add(k);
l.addAll(list.get(j));
ans.add(l);
}
}
}
}else if (k==target) {
List<Integer> l=new ArrayList<Integer>();
l.add(k);
ans.add(l);
}else {
break;
}
}
map.put(target, ans);
return ans;
}
return map.get(target);
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: