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.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
随机推荐
- cordova 入门
1. npm install -g cordova On Windows, npm can usually be found at C:\Users\username\AppData\Roaming\ ...
- 入参是小数的String,返回小数乘以100的String
String money = request.getParameter("orderAmt"); BigDecimal moneyDecimal = new BigDecimal( ...
- (2.4)备份与还原--WAL与备份原理
预写式日志(Write-Ahead Logging (WAL)) 部分转自:http://www.cnblogs.com/wenBlog/p/4423497.html SQL Server中使用了W ...
- PAT 1122 Hamiltonian Cycle[比较一般]
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 随机深林和GBDT
随机森林(Random Forest): 随机森林是一个最近比较火的算法,它有很多的优点: 在数据集上表现良好 在当前的很多数据集上,相对其他算法有着很大的优势 它能够处理很高维度(feature很多 ...
- 无法从U盘启动的解决方案
联想台式机无法从U盘启动的解决方案 F1进入lenovo bios 选择 StartUp 选项卡 1) 发现 USB FDD 已处于第一项,再把 USB Key 调到启动第二项 2) 把 boot m ...
- day6-面向对象
Python 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没有接触过 ...
- SuperObject使用
SuperObject SuperObject第三方控件包,试用了下并编写了个DEMO,发现还是蛮强大.废话 少说,看看下面的代码就知道其强大: 1.JSON原始数据 { "name&q ...
- Zen cart 根据数量打折插件
Quantity Discounts 可以根据顾客购买多少来打折,很不错. 假如顾客买了3个以上的产品,就给他10%折扣,设置如下: Turn On Quantity Discount 1. In t ...
- Redis持久化及复制
一.持久化的两种方式 1.RDB: RDB是在指定时间间隔内生成数据集的时间点快照(point-in-time snapshot)持久化,它是记录一段时间内的操作,一段时间内操作超过多少次就持久化.默 ...