[英]How arraylist operate and is reused in subsets algorithms
I do not understand how ArrayList operate in this problem.我不明白 ArrayList 在这个问题上是如何运作的。
This is a algorithm for the question called "Subsets" from leetcode.这是 leetcode 中名为“子集”的问题的算法。
It basically outputs all the subsets of given array.它基本上输出给定数组的所有子集。
For example, if [1,2,3] is given array, then the output should be [[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]例如,如果给定数组 [1,2,3],则 output 应为 [[],[1],[2],[3],[1,2],[1,3],[2 ,3],[1,2,3]]
This is an algorithm for this question, but I do not understand how arraylist work in this code.这是这个问题的算法,但我不明白 arraylist 在这段代码中是如何工作的。
import java.util.*;
class Subsets {
public static List<List<Integer>> findSubsets(int[] nums) {
List<List<Integer>> subsets = new ArrayList<>();
// start by adding the empty subset
subsets.add(new ArrayList<>());
for (int currentNumber : nums) {
// we will take all existing subsets and insert the current number in them to create new subsets
int n = subsets.size();
for (int i = 0; i < n; i++) {
// create a new subset from the existing subset and insert the current element to it
List<Integer> set = new ArrayList<>(subsets.get(i));
set.add(currentNumber);
subsets.add(set);
}
}
return subsets;
}
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);
result = Subsets.findSubsets(new int[] { 1, 2, 3 });
System.out.println("Here is the list of subsets: " + result);
}
}
The exact code part that I am stuck我被卡住的确切代码部分
int n = subsets.size();
for (int i = 0; i < n; i++) {
List<Integer> set = new ArrayList<>(subsets.get(i));
set.add(currentNumber);
subsets.add(set);
}
}
Say n is 2 and currentNumber is 2 as well.假设 n 为 2,currentNumber 也为 2。 In that literation, I think it just creates [2],[2] instead of [2],[1,2] since every inner loop iteration, it creates a new ArrayList and has a same value as currentNumber.在那个文献中,我认为它只是创建 [2],[2] 而不是 [2],[1,2],因为每次内部循环迭代,它都会创建一个新的 ArrayList 并且具有与 currentNumber 相同的值。 I do not understand where [1,2] is from.我不明白 [1,2] 来自哪里。
Can anyone explain how it outputs [2], [1,2] in second outer literation or where I do not understand?谁能解释它如何在第二个外部文字中输出 [2]、[1,2] 或我不明白的地方?
To understand where the [1]
comes from, we start with the first iteration:为了理解[1]
的来源,我们从第一次迭代开始:
subsets.add(new ArrayList<>());
adds the empty list to the set;将空列表添加到集合中;
so, if currentNumber
is 1
(and size of set is 1 too), it will add 1
to a copy of the empty list from the set ( get(0)
), resulting in [1]
which is then added to the set.因此,如果currentNumber
为1
(集合的大小也为 1),它会将1
添加到集合( get(0)
)中的空列表的副本中,从而导致[1]
然后将其添加到集合中。
Now we have the condition posted in question: the set is [[],[1]]
, size 2 ( n = 2
) and next number will be 2
( currentNumber = 2
).现在我们发布了有问题的条件:集合是[[],[1]]
,大小为 2 ( n = 2
),下一个数字是2
( currentNumber = 2
)。
subsets
has two elements ( n == 2
): the empty list, and a list containing just 1
(from previous iteration). subsets
有两个元素( n == 2
):空列表和仅包含1
的列表(来自上一次迭代)。
subsets = [[], [1]]
Now,现在,
iteration i = 0
, is adding 2
to a copy of the empty list ( get(0)
) and adding the result ( [2])
to subsets
;迭代i = 0
,将2
添加到空列表的副本( get(0)
)并将结果( [2])
添加到subsets
;
iteration i = 1
, is adding 2
to a copy of [1]
( get(1)
), result [1,2]
is added to subsets
.迭代i = 1
,将2
添加到[1]
的副本( get(1)
),结果[1,2]
被添加到subsets
。
subsets
now is [],[1],[2],[1,2]
- next iteration will add 3
to a copy of every one of that four lists and add the resulting 4 lists to subsets
. subsets
现在是[],[1],[2],[1,2]
- 下一次迭代将向这四个列表中的每一个的副本添加3
,并将生成的 4 个列表添加到subsets
。
Key of that algorithm is new ArrayList<>(subsets.get(i))
which is creating a new list containing the elements of the list already in the set - a clone.该算法的关键是new ArrayList<>(subsets.get(i))
它正在创建一个新列表,其中包含已经在集合中的列表元素 - 一个克隆。 Otherwise the end result would contain the same list repeated (with same content).否则,最终结果将包含重复的相同列表(具有相同内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.