1、在使用回溯算法时,循环中,对于数组和字符串的处理是不一样的
数组是在回溯时index=i+1,而字符串是在循环时便加1,然后回溯使用的index=i。(子集使用的是i+1)
2、似乎使用new ArrayList<>(LinkedList)可以将链表转成一个新的线性表(这是因为ArrayList是直接指向地址,后续的操作也会影响到前面的结果)
同理在该题中,利用new ArrayList<>(HashSet)也可以将哈希set转成一个新的线性表(该题更多的是注重形式上的转化)
class Solution {
Set<List<Integer>> result = new HashSet<>();
LinkedList<Integer> temp = new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
helper(nums, 0);
return new ArrayList<>(result);
}
public void helper(int[] nums, int index) {
result.add(new ArrayList<>(temp));
if(index == nums.length ) {
//result.add(new ArrayList<>(temp));
return;
}
for(int i=index;i<nums.length;i++) {
//result.add(new ArrayList<>(temp));
temp.add(nums[i]);
helper(nums, i+1);
temp.removeLast();
}
}
}