[抄题]:

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]:

[思维问题]:

[一句话思路]:

不排序也行,但最好还是排一下

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. backtracing函数是需要来回迭代的。每次不是从0开始,是从start开始,start也是要反复换的 要变成i + 1
  2. 主函数中和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)的更多相关文章

  1. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  2. LeetCode 78 Subsets (所有子集)

    题目链接:https://leetcode.com/problems/subsets/#/description   给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...

  3. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. 刷题78. Subsets

    一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...

  5. 【一天一道LeetCode】#78. Subsets

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. hdu_2668 Daydream O(n)求最长不重复子串

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668 Daydream Time Limit: 2000/1000 MS (Java/Others)  ...

  7. 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 ...

  8. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  9. LeetCode 90 | 经典递归问题,求出所有不重复的子集II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第56篇文章,我们一起来看看LeetCode第90题,子集II(Subsets II). 这题的官方难度是Medi ...

随机推荐

  1. FastAdmin 开发时出错的调试

    第一步:打开 FastAdmin Debug 打开 Debug. 第二步:打开浏览器控制器 打开浏览器控制器,并按 F5 重新加载页面. 第三步:在 network 查看详细错误信息 点击红色链接,可 ...

  2. CentOS6.5安装中文支持

    本人在安装CentOS6.5时选择是英文版,安装后打开文档,发现好些文档成了乱码了. 这个问题的原因是没有中文支持. 解决方法: 1.安装中文支持包 # yum groupinstall " ...

  3. atoi函数的实现——面试

    主要考虑,字符串中是否有非法字符,字符串是否有溢出控制 #include<stdio.h> int myatoi(const char *str){ ,ret=,i=; if(str[i] ...

  4. 常见企业IT支撑【4、gitlab代码管理工具】

    安装方式可借鉴http://www.cnblogs.com/juandx/p/5339254.html 安装方式

  5. SQL Server数据库常用的T-SQL命令

    1. 查看数据库的版本 select @@version 2.查看数据库所在机器操作系统参数 exec master..xp_msver 3. 查看数据库启动的参数 sp_configure 4.查看 ...

  6. Linux编译前提前丰富库资源

    Linux在软件编译的时候,时常提示一些依赖,无谓浪费时间.我们可以事先将常用的依赖包,一起安装一下,防止后续编译过程被打断. 之前,有个很重要的前提,就是epel源的安装. # ls /etc/yu ...

  7. Mac运维安装软件

    Maccrt软件 sudo spctl --master-disable 打开软件,复制到app,按照sn.txt输入即可 sudo spctl --master-enable crt快捷键crtl ...

  8. 无线加密的多种方法及其区别(WEP WPA TKIP EAP)

    无线加密的多种方法及其区别(WEP WPA TKIP EAP) 无线网络的安全性由认证和加密来保证. 认证允许只有被许可的用户才能连接到无线网络: 加密的目的是提供数据的保密性和完整性(数据在传输过程 ...

  9. 电话号码以185****3547显示demo

    String phone="18678473547"; //String phones = phone.substring(0,phone.length()-(phone.subs ...

  10. 0007-一套完整的CRUD_DEMO

    好久没写了,一直在忙别的东西,但是想想,还是把之前的补充完整好了.给大家一个参考,也为自己留个备份. 首先写一个Html作为内容载体,主要结构如下 <div ui-view="navb ...