向量内的向量
我有这段代码...它几乎完全符合我的需要。它在预定义的整数数组中搜索两个总和为目标整数的整数。但是,当将值放入向量时,不是将它们放入单元格内,而是将所有值放在一起。 即对于 int array[50,40,30,20,10] 和目标 50,它不是返回 [[50][40,10][30,20]...etc.],而是打印 [[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;
}
I have this code...that does just about exactly what I need it to. It searches a predefined array of ints for two ints that sum up to a target int. However, when putting values into the vector, rather than placing them within cells, it places all the values together.
i.e. for int array[50,40,30,20,10] and target 50, rather than returning [[50][40,10][30,20]...etc.], it prints [[50,40,10,30,20...etc.]] How can I fix this?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只需向
outer
添加一个向量。如果您找到一对加起来达到所需总和的值,您是否希望它们位于不同的向量中?因此,当发生这种情况时,您需要创建一个新的“内部”向量,并将其添加到outer
。删除这些行:
Change:
And:
最后,考虑将
inner
和outer
设为局部变量。You're only ever adding a single vector to
outer
. Isn't it the case that if you find a pair that add up to the required sum, you want them in a distinct vector? So, you need to create a new "inner" vector when that happens, and add it toouter
.Remove these lines:
Change:
And:
Finally, consider making
inner
andouter
into local variables.我认为你的错误在于首先在外部向量中添加向量值,因此它只返回外部向量中的所有值而不是内部向量中的值。必须在检查条件后添加
I think your mistake lies in add vector value in outer at very first so its return all the value from outer vector only not in the inner vector.. have to added after checking condition
将这两行:
移至外循环(带有索引变量
k
的循环。)Move these two lines:
into the outer loop (the one with index variable
k
.)另外,你打算有一个向量的向量吗?因为目前,您有一个向量,其中有一个名为“inner”的向量,因此所有内容都将直接添加到“inner”中。您并不是每次都创建一个新向量来放入每一对。
Also, are you intending to have a vector of vectors? because at the moment, you have a vector, with one vector named inner in it, so everything is being added straight into inner. You are not creating a new vector each time to put each pair in.
Deepa 的答案已经完成了一半,但是您还需要在
for
循环内创建一个新的inner
实例,然后再向其中添加值;否则,您最终会得到一个inner
内的所有值。像这样:您还可以在添加后
继续
以稍微提高性能。Deepa's answer is half-way there, but you'll also need to create a new instance of
inner
inside thefor
loop before adding the values to it; otherwise, you'll end up with all the values inside of oneinner
. Like this:You can also
continue
after adding to improve performance a bit.