public static ArrayListresult =new ArrayList();
public static Listtask1(int n,int k){
back(n,k,new ArrayList(),1);
return result;
}
public static void back(int n, int k, ArrayList path,int start){
if(path.size() == k && n ==0){
result.add(new ArrayList<>(path));
return;
}
if(n<0){
return;
}
for(int i = start;i<=9;i++){
path.add(i);
back(n-i,k,path,i+1);
path.remove(path.size()-1);
}
}
}
网友评论