LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2
题目:矩阵置0
难度:Easy
题目内容:
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
翻译:
给定一组不同的整数,nums,返回所有可能的子集(包括空集和自身)。
注意:解决方案集不能包含重复的子集。
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
我的思路:无。。。。。
答案代码:
public List<List<Integer>> subsetsWithDup(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]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
答案复杂度:O(n2)
答案思路:
其实就是利用回溯递归的思想,单独写一个回溯方法,对每一个子集都是由各个字母开头组成的(子集的子集的子集。。。)这样一个集合,
所以方法内只需要先将tempList加入结果集List,然后写一个循环,将从start开始,依次将当前nums[ i ] 加入tempList,然后递归调用回溯方法(start= i+1),调用完毕后说明以nums[ i ]开头的子集结束,所以将nums[ i ]从tempList中移出。
下面是流程:以输入【1,2,3】为例(在回溯方法第一行打印tempList即可看见)
[]———————从空开始
[1]——————以空开头的第一个
[1, 2]——————以1开头的第一个
[1, 2, 3]——————以12开头的第一个,此时到3了,说明以12开头的子集结束,删掉2,此时回溯到以1开头
[1, 3]——————以1开头的第二个,此时又到3,以1开头的子集结束,删掉1,回溯到以空开头
[2]——————以空开头的第二个
[2, 3]——————以2开头的第一个,此时到3,以2开头的子集结束,删掉2,回溯到以空开头
[3]——————以空开头的最后一个

扩展:当nums包含重复字符的时候应该怎么办?第[90]题:Subsets 2
1、一开始是乱序的所以需要将nums排序才能判断是否重复。
2、在回溯方法的循环里第一行加入重复判断,如果当前元素(非第一个)和上一个元素一样,那么就跳过此元素不使用递归。
public List<List<Integer>> subsetsWithDup(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++){
if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2的更多相关文章
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode(78):子集
Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...
- 【LeetCode】78. Subsets (2 solutions)
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
随机推荐
- Linux(5)- MariaDB、mysql主从复制、初识redis
一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...
- 《Python核心编程》第五章:数字
PS:[笔记+代码+图片]在GitHub上持续更新,欢迎star:https://github.com/gdouchufu/Core-Python-Programming 本章大纲 介绍Python支 ...
- jquery prop attr
checked比较特殊,只要设置了属性checked,不管何值都是checked的.例如:<input type="checkbox" checked><inpu ...
- django+celery 实现定时任务
利用 celery 实现定时任务 celery支持定时任务,设定好任务的执行时间,celery就会定时自动帮你执行, 这个定时任务模块叫celery beat Celery安装 由于celery 4. ...
- python .bat
传值给.bat os.system('%s %s %s %s %s' % ('image_dispose.bat', change_photo,dic['width'], '-resize', cha ...
- DEPHI XE5 XE6 ANDROID IOS开发的几点体会
DEPHI XE5 XE6 ANDROID IOS开发的几点体会 2014-09-04 20:48 1.不纠结于APK或者APP的大小.现在的客户端设备都很廉价,300元以上的新安卓设备都不在乎软件的 ...
- android 读取通讯录显示到gridview
........... <GridView android:id="@+id/gridView1" android:layout_width="match_pare ...
- git安装教程(windows安装)
git下载地址 https://git-scm.com/download/win 选择安装的组件,推荐全选 Additional icons 附加图标 On the Desktop 在桌面上 Wi ...
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...
- HDU - 4609 3-idiots (FFT+母函数)
题意:给N个数,求任意选三个数能构成三角形的概率 分析:枚举两条边之和的复杂度\(O(N^2)\),显然不行,所以要更高效地做到枚举出两边之和. 所以用生成函数搭配FFT在\(O(NlogN)\)的时 ...