import java.util.*;
public class Solution {
// 方法二:通过队列迭代遍历
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
Arrays.sort(S);
LinkedList<ArrayList<ArrayList<Integer>>> result = new LinkedList<>();
ArrayList<ArrayList<Integer>> first = new ArrayList<>();
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(S[0]);
first.add(arrayList);
result.add(first);
for (int i = 1; i < S.length; i++) {
ArrayList<Integer> f = new ArrayList<>();
f.add(S[i]);
ArrayList<ArrayList<Integer>> secend = new ArrayList<>();
secend.add(f);
ArrayList<ArrayList<Integer>> t = result.getLast();
for (ArrayList<Integer> ret : t) {
ArrayList<Integer> t1 = new ArrayList<>(ret);
t1.add(S[i]);
secend.add(t1);
secend.add(ret);
}
result.add(secend);
result.removeFirst();
}
result.getLast().sort(new Comparator<ArrayList<Integer>>() {
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for (Integer value : o1) {
a.append(value);
}
for (Integer value : o2) {
b.append(value);
}
return a.toString().compareTo(b.toString());
}
});
result.getLast().add(0, new ArrayList<>());
return result.getLast();
}
}