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给出的题目变变变]:
[代码风格] :
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);
}
}
}
78. Subsets 求所有子集(有重复就continue)的更多相关文章
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- LeetCode 78 Subsets (所有子集)
题目链接:https://leetcode.com/problems/subsets/#/description 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 刷题78. Subsets
一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...
- 【一天一道LeetCode】#78. Subsets
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- hdu_2668 Daydream O(n)求最长不重复子串
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668 Daydream Time Limit: 2000/1000 MS (Java/Others) ...
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode 90 | 经典递归问题,求出所有不重复的子集II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第56篇文章,我们一起来看看LeetCode第90题,子集II(Subsets II). 这题的官方难度是Medi ...
随机推荐
- centos安装 node.js
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - yum clean all yum makeca ...
- Docker安装ShowDoc
ShowDoc就是一个非常适合IT团队的在线文档分享工具,它可以加快团队之间沟通的效率. 一.下载showDoc资源 打开 https://github.com/star7th/showdoc 复制其 ...
- Python——基础知识
1. 运行Python文件 python(空格)文件路径 回车 2. 注释 (1)单行注释:# #注释内容 print('abc') #abc print("abc") #abc ...
- STL容器的resize方法
刚才写DFS程序,为了加个创建DFS树然后再次遍历的功能,把初始化的代码给封装到init()函数里,然后直接调用之后对同样是邻接表表示的DFS树进行DFS. 然后令人诧异的是竟然没有打印结果,调试发现 ...
- python算两个时间之间的天数,将天数转成int型
import time import datetime #计算两个日期相差天数,自定义函数名,和两个日期的变量名. def Caltime(date1,date2): #%Y-%m-%d为日期格式,其 ...
- vscode新版1.31.1使用代码检查工具ESlint支持VUE
1.VSCODE中安装ESlint省略 2.菜单文件->首选项->设置->扩展->ESLint 打钩:Eslint:Auto Fix On Save 点击此链接:在settin ...
- Julia - 分数
在 Julia 中,使用“//”运算符构造分数 julia> 1 // 2 1//2 julia> - 1 // 2 -1//2 分数会自动进行约分 julia> 2 // 4 1/ ...
- postman 中url有动态变换的值时,可以按下面方式变换。
get 和post均适用.
- C# 设计模式-单例模式(Singleton)
所谓单例模式即所谓的一个类只能有一个实例,说白了,也就是类只能在内部实例一次,然后提供这一实例,外部无法对此类实例化. 单例模式的特点: 1.只能有一个实例: 2.只能自己创建自己的唯一实例: 3.必 ...
- IOS Background 之 Background Fetch
http://www.ithao123.cn/content-1363653.html 定期更新数据的app,比如及时通信类,微博等app. 定期后台获取,等打开后获取的快一些. 30分钟后打开手,获 ...