首页 技术 正文
技术 2022年11月15日
0 收藏 759 点赞 4,183 浏览 2680 个字

[抄题]:

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);
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898