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 ...
随机推荐
- Centos6.5 恢复误删的系统面板
在CentOS6.5下往面板上拖应用程序时,手贱了,点了"Delete This Panel".结果就悲剧了~面板不见了! 从网上搜了一下解决方法,列举一下. 1.新建面板 如果下 ...
- vs2017 xamarin导入jar,SO文件的问题
最近要弄用vs弄个安卓的系统,因为要使用硬件,所以要引进jar,SO文件 导入jar文件很顺利,具体步骤我也是在网上找的这里给个链接 http://www.2cto.com/kf/201604/502 ...
- angular的require模块的使用($requireProvider的作用)
今天我们学习一下angular的另一个几乎被忽略的模块angular-require 先给出链接地址(git:) https://github.com/Treri/angular-requir ...
- Hadoop的简单序列化框架
Hadoop提供了一个加单的序列化框架API,用于集成各种序列化实现.该框架由Serialization实现. 其中Serialization是一个接口,使用抽象工厂的设计模式,提供了一系列和序列化相 ...
- RK3288 查看时钟树
主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...
- 历届试题 小数第n位(小技巧)
问题描述 我们知道,整数做除法时,有时得到有限小数,有时得到无限循环小数. 如果我们把有限小数的末尾加上无限多个0,它们就有了统一的形式. 本题的任务是:在上面的约定下,求整数除法小数点后的第n位开始 ...
- mac 电脑下添加 HTMLtestrunner.py 生成 报表
HTMLTestRunner是Python标准库unittest模块的一个扩展.它生成易于使用的HTML测试报告. 1.下载HTMLTestRunner.py模块地址 http://tungwaiyi ...
- Java安全 – JCE Blowfish算法报错
代码里用Blowfish算法加解密,结果jdk升到1.7后算法初始化失败 java.lang.RuntimeException: java.lang.RuntimeException: PANIC: ...
- Tkinter学习
from tkinter import * window = Tk() # 创建一个窗口 window.mainloop() # 消息循环,显示窗口 window.title("窗口标题&q ...
- node中express的中间件之cookieParser
cookieParser中间件用于获取web浏览器发送的cookie中的内容.在使用了cookieParser中间件后, 代表客户端请求的htto.IncomingMessage对象就具有了一个coo ...