Java回溯法解法
加起来和为目标值的组合
http://www.nowcoder.com/questionTerminal/75e6cd5b85ab41c6a7c43359a74e869a
import java.util.*; public class Solution { public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> arr = new ArrayList<Integer>(); if(num == null || num.length==0 || target<0)return res; Arrays.sort(num); dfs(num,target,res,arr,0); return res; } void dfs(int[] num,int target,ArrayList<ArrayList<Integer>> res,ArrayList<Integer> arr,int start){ if(target==0){ res.add(new ArrayList<Integer>(arr)); return; } if(start >= num.length)return; for(int i=start;i<num.length;i++){ if(i > start && num[i] == num[i-1])continue; if(num[i] <= target){ arr.add(num[i]); dfs(num,target-num[i],res,arr,i+1); arr.remove(arr.size()-1); } } return; } }