[英]I cant figure out some part in my code, queue and java
import java.util.*;
class Subsets {
public static List<List<Integer>> findSubsets(int[] nums){
List<List<Integer>> result = new ArrayList<>();
Queue<List<Integer>> queue = new LinkedList<>();
queue.add(new ArrayList<>()); // add empty set to queue
result.add(new ArrayList<>()); //add empty set to result
for(int i=0; i<nums.length; i++){
while(!queue.isEmpty()){
System.out.println("current result = " + result);
List<Integer> temp = queue.poll();
System.out.println("current temp = " + temp);
System.out.println("before change temp, current result = " + result);
temp.add(nums[i]);
System.out.println(i + " add index i value to temp, i= " + temp);
System.out.println("not yet add to result, current result = " + result);
result.add(temp);
System.out.println("after add temp to result, result = " + result);
}
//add all elements in result to queue
int j=0;
while(j < result.size()){
queue.add(result.get(j));
j++;
}
}
return result;
}
public static void main(String[] args) {
List<List<Integer>> result = Subsets.findSubsets(new int[] { 1, 3 });
System.out.println("Here is the list of subsets: " + result);
}
}
and here is output of the code 这是代码的输出
current result = [[]]
current temp = []
before change temp, current result = [[]]
0 add index i value to temp, i= [1]
not yet add to result, current result = [[]]
after add temp to result, result = [[], [1]]
current result = [[], [1]]
current temp = []
before change temp, current result = [[], [1]]
1 add index i value to temp, i= [3]
not yet add to result, current result = [[3], [1]]
after add temp to result, result = [[3], [1], [3]]
current result = [[3], [1], [3]]
current temp = [1]
before change temp, current result = [[3], [1], [3]]
1 add index i value to temp, i= [1, 3]
not yet add to result, current result = [[3], [1, 3], [3]]
after add temp to result, result = [[3], [1, 3], [3], [1, 3]]
Here is the list of subsets: [[3], [1, 3], [3], [1, 3]]
I know this is kind of dirty code, but rather than just think better way, I need to understand some part I still can't figure it out. 我知道这是一种肮脏的代码,但我不只是想更好的方法,还需要了解一些我仍然无法弄清楚的部分。
This is code to get subsets of the given set.
这是获取给定集合子集的代码。 For example, when we are given {1,3} then output should be {}, {1}, {3}, {1,3}
例如,当给定{1,3}时,输出应为{},{1},{3},{1,3}
I try to solve this question to use queue, but my point is that you can see second paragraph of the result output 我尝试解决使用队列的问题,但我的观点是您可以看到结果输出的第二段
before change temp, current result = [[], [1]]
1 add index i value to temp, i= [3]
not yet add to result, current result = [[3], [1]]
You definitely can see the point when you look at my code, I do nothing on result, I just add new value to temp, but result suddenly changed. 当您查看我的代码时,您肯定可以看到要点,我对结果什么也不做,我只是向temp添加新值,但是结果突然改变了。
I am not able to figure out what I did wrong or I may have wrong basis on queue? 我无法弄清楚我做错了什么,或者队列中的依据可能不正确?
To fix the error you have, you must understand that you are adding the same list reference to the results and the queue multiple times and thus modifying those same lists over and over again. 要解决您所遇到的错误,您必须了解要多次向结果和队列添加相同的列表引用,从而一次又一次地修改这些相同的列表。
Changing queue.add(result.get(j));
更改
queue.add(result.get(j));
to queue.add(new ArrayList<>(result.get(j)));
到
queue.add(new ArrayList<>(result.get(j)));
, makes a new list that is a copy of the results list passed to it (not a reference to the same list like before). ,将创建一个新列表,该列表是传递给它的结果列表的副本(而不是像以前一样引用同一列表)。 Because it is now copied, modifications later on like
temp.add(nums[i]);
由于现在已复制,因此稍后进行修改,例如
temp.add(nums[i]);
no longer modify the result's lists. 不再修改结果列表。
import java.util.*;
class Subsets {
public static List<List<Integer>> findSubsets(int[] nums){
List<List<Integer>> result = new ArrayList<>();
Queue<List<Integer>> queue = new LinkedList<>();
queue.add(new ArrayList<>()); // add empty set to queue
result.add(new ArrayList<>()); //add empty set to result
for(int i=0; i<nums.length; i++){
while(!queue.isEmpty()){
System.out.println("current result = " + result);
List<Integer> temp = queue.poll();
System.out.println("current temp = " + temp);
System.out.println("before change temp, current result = " + result);
temp.add(nums[i]);
System.out.println(i + " add index i value to temp, i= " + temp);
System.out.println("not yet add to result, current result = " + result);
result.add(temp);
System.out.println("after add temp to result, result = " + result);
}
//add copy of all elements in result to queue
int j=0;
while(j < result.size()){
queue.add(new ArrayList<>(result.get(j)));
j++;
}
}
return result;
}
public static void main(String[] args) {
List<List<Integer>> result = Subsets.findSubsets(new int[] { 1, 3 });
System.out.println("Here is the list of subsets: " + result);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.