我有这个代码......这正是我需要的。它在一个预定义的整数数组中搜索总和为目标整数的两个整数。但是,当将值放入向量中时,它不是将它们放在单元格中,而是将所有值放在一起。即对于 int array[50,40,30,20,10] 和目标 50,而不是返回 [[50][40,10][30,20]...等],它打印 [[50,40 ,10,30,20...]] 我该如何解决这个问题?
public Vector<Vector<Integer>> subsetSum(int[] array, int target) {
//creates vectors, adds inner vector to another vector
outer = new Vector<Vector<Integer>>();
inner = new Vector<Integer>();
outer.add(inner);
for (int k = 0; k < array.length; k++) {
for (int l = 1; l < array.length; l++) {
int sum = array[k]+array[l]; //sum of l and k
if (sum == target) {
//add l,k to vector
inner.add(array[l]);
inner.add(array[k]);
//prints l and k if their sum equals target
System.out.println(array[l]+"+"+array[k]+"="+target);
}
else {
System.out.print("");
}
}
//if k is the target, display
if (array[k] == target) {
//add k to vector
inner.add(array[k]);
//prints if int equals target
System.out.println(array[k]+"="+target);
}
}
//return combinations that add up to target in vector form
return outer;
}