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.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
随机推荐
- 007-Hadoop Hive sql语法详解2-修改表结构
一.表 更改表名:ALTER TABLE table_name RENAME TO new_table_name 增加表的元数据信息:ALTER TABLE table_name SET TBLPRO ...
- django 增加验证邮箱功能
在user文件夹下新建python包,utils 在包内新建文件email_send.py,其中包括验证字符串随机码的产生,数据库的存储和email的发送 # -*- coding: utf-8 -* ...
- 模块讲解----shutil模块(copy、压缩、解压)
作用与功能 主要用于文件的copy,压缩,解压 导入shuitl模块: import shutil copy方法 1.shutil.copyfileobj() 打开file1,并copy写入file ...
- Python 新手常犯错误(第一部分)转载
觉得这篇文章针对python的默认参数写的不错,翻译的也不错,故转载下. 原文链接: Amir Rachum 翻译: 伯乐在线- 伯乐在线读者译文链接: http://blog.jobbole.c ...
- java环境变量及Eclipse自动编译问题
环境变量,是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在的完整路 ...
- u-boot.cfg转eclipse_xml小脚本
手动复制粘贴版本 cat u-boot.cfg | awk '{if(length($3)){$3 = substr($0, length($1)+length($2)+3); gsub(" ...
- ARKit 研究笔记一
软件需求:Xcode9.x .blender 硬件需求:iphone 6s + 系统:iOS 11 + 技能储备: ARKit .SceneKit(苹果提供的3d游戏库) 或 SpriteKit(苹果 ...
- tomcat源码调试2
前面对tomcat做了一些简单的认识,下面将tomcat源码调试环境搭建起来. 可以参考官网的搭建方法,这里是按照网上的maven管理的方式搭建. 大概步骤是: 1.下载tomcat 9的源码,一般是 ...
- qml源码查看
已5.4为例说明: QtQuick源码查看: 地址:Qt\Qt5.4.1\5.4\Src\qtdeclarative\src\quick\items Qt control源码查看: 地址:\Qt\Qt ...
- React 常用插件库
js 加密 crypto-js (des加密,md5) crypto-js https://www.npmjs.com/package/crypto-js Mock联调 数据是前端开发过程中必不可少的 ...