public class Solution {
/**
* @param A: an integer array.
* @param k: a positive integer (k <= length(A))
* @param target: a integer
* @return a list of lists of integer
*/
static ArrayList<ArrayList<Integer>> arraylist = new ArrayList<ArrayList<Integer>>();
static ArrayList<Integer> neiArrayList =new ArrayList<Integer>();
public static ArrayList<ArrayList<Integer>> choice(int[] arry,int n,int m,int M,int[] b,int target) {
int sum=0;
for(int i=n;i>=m;i--){
if(m>1){
b[m-1]=i-1;
choice(arry, i-1, m-1, M, b,target);//组合为 choice(arry, i-1, m-1, M, b);
}
else {
ArrayList<Integer> tempArrayList =new ArrayList<Integer>();
b[0]=i-1;
for(int j=M-1;j>=0;j--){
neiArrayList.add(arry[b[j]]);
sum+=arry[b[j]];
}
for(int k=0;k<neiArrayList.size();k++)
{
tempArrayList.add(neiArrayList.get(k));
}
neiArrayList.clear();
if(sum==target)
arraylist.add(tempArrayList);
sum=0;
}
}
return arraylist;
}
public ArrayList<ArrayList<Integer>> kSumII(int A[], int k, int target) {
int[] b=new int[100];
int n=A.length;
int m=k;
int M=k;
return choice(A,n,m,M,b,target);
}
}