78. Subsets 求所有子集(有重复就continue)
[抄题]:
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
不排序也行,但最好还是排一下
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- backtracing函数是需要来回迭代的。每次不是从0开始,是从start开始,start也是要反复换的 要变成i + 1
- 主函数中和backtrace函数中,第一回添加tempList 需要new一个新的
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
第一次用的东西都要new一个新的
[复杂度]:Time complexity: O(2^n like word break, each numbe can be added or not) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
背诵backtracing的模板,具体实现如下:
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
System.out.println("add了nums1[i]="+nums[i]);//
System.out.println("扩大的tempList.size1() ="+tempList.size());//
System.out.println("-------------");//
backtrack(list, tempList, nums, i + 1);
System.out.println("要remove nums2[i]="+nums[i]);//
tempList.remove(tempList.size() - 1);
System.out.println("缩小的tempList.size2() ="+tempList.size());//
System.out.println("-------------");//
}
}
}
add了nums1[i]=1
扩大的tempList.size1() =1
-------------
add了nums1[i]=2
扩大的tempList.size1() =2
-------------
add了nums1[i]=3
扩大的tempList.size1() =3
-------------
要remove nums2[i]=3
缩小的tempList.size2() =2
-------------
要remove nums2[i]=2
缩小的tempList.size2() =1
-------------
add了nums1[i]=3
扩大的tempList.size1() =2
-------------
要remove nums2[i]=3
缩小的tempList.size2() =1
-------------
要remove nums2[i]=1
缩小的tempList.size2() =0
-------------
add了nums1[i]=2
扩大的tempList.size1() =1
-------------
add了nums1[i]=3
扩大的tempList.size1() =2
-------------
要remove nums2[i]=3
缩小的tempList.size2() =1
-------------
要remove nums2[i]=2
缩小的tempList.size2() =0
-------------
add了nums1[i]=3
扩大的tempList.size1() =1
-------------
要remove nums2[i]=3
缩小的tempList.size2() =0
-------------
[关键模板化代码]:
[其他解法]:
[Follow Up]:
有重复:排序+continue
[LC给出的题目变变变]:
[代码风格] :
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
//ini: sort
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
//cc
if (nums == null || nums.length == 0) return res;
//dfs
backtrace(nums, new ArrayList<>(), 0, res);
//return
return res;
}
//dfs
public void backtrace(int[] nums, List<Integer> tempList, int start, List<List<Integer>> res) {
//res.add(tempList);
res.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) continue;
tempList.add(nums[i]);
backtrace(nums, tempList, i + 1, res);
tempList.remove(tempList.size() - 1);
}
}
}