个人感觉和leetcode上的39、40整体思路是差不多的,只要那几道组合过了这个也不难,index变量的存在保证了只向后查找或添加元素,避免重复
菜鸡做法:
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
solution(-1, nums, ans, new LinkedList<>());
return ans;
}
public void solution(int index, int[] nums, List<List<Integer>> ans, LinkedList<Integer> list){
//存储结果
ans.add(new LinkedList<>(list));
for(int i = index + 1;i < nums.length;i ++){
list.addLast(nums[i]);
solution(i, nums, ans, list);
list.removeLast();
}
}
}