[Leetcode 78]求子集 Subset
【题目】
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
求子集,无重复数
【思路】
1、有回溯法模板
2、将tmp加入进ans。tmp用来存放第i个元素的子集。回溯产生第i个元素与其后[i+1,len]元素产生的子集(i-1前已经产生过,不重复遍历)。因第i个元素所有子集已经生成并加入到ans中,移除tmp这个元素,为下次add做准备。。
3、子集产生的顺序无关
【相关题目】
1、[Leetcode 78]求子集 Subset https://www.cnblogs.com/inku/p/9976049.html
2、[Leetcode 90]求含有重复数的子集 Subset II https://www.cnblogs.com/inku/p/9976099.html
3、讲解在这: [Leetcode 216]求给定和的数集合 Combination Sum III
【代码】
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans=new ArrayList<>();
List<Integer> tmp=new ArrayList<>();
Arrays.sort(nums);
fun(ans,tmp,nums,);
return ans;
}
public void fun(List<List<Integer>> ans, List<Integer> tmp,int[] nums,int flag){
ans.add(new ArrayList<>(tmp));
for(int i=flag;i<nums.length;i++){
tmp.add(nums[i]);
fun(ans,tmp,nums,i+);
tmp.remove(tmp.size()-);
}
}
}
[Leetcode 78]求子集 Subset的更多相关文章
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- [Leetcode 90]求含有重复数的子集 Subset II
[题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- SCU 4424(求子集排列数)
A - A Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice ...
- 基于visual Studio2013解决面试题之1309求子集
题目
- LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2
题目:矩阵置0 难度:Easy 题目内容: Given a set of distinct integers, nums, return all possible subsets (the pow ...
- LeetCode 78 Subsets (所有子集)
题目链接:https://leetcode.com/problems/subsets/#/description 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
随机推荐
- 为archlinux终端ls不同类型文件设置不同显示颜色
title: 为archlinux终端ls不同类型文件设置不同显示颜色 date: 2017-11-13 20:53:55 tags: linux categories: linux archlinu ...
- mongodb 安装遇到问题:the domain,user name and/or password are incorrect.remember to use"." for the domain if the account is on the local machine
安装mongoDB遇到如下问题:the domain,user name and/or password are incorrect.remember to use"." for ...
- Windows磁盘映射读写远程主机文件
执行CMD命令做磁盘映射:net use X: \\172.17.0.1\D$\test Password /USER:Administrator Java调用CMD String cmd = &qu ...
- hdu5236 Article
题目链接 概率DP $dp_i$表示连打$i$个字符的期望按键数 那么枚举保存的次数,均分一下连打的个数就好 #include<iostream> #include<cstdio&g ...
- 1.1:Get Started with Unity Shaders
文章著作权归作者所有.转载请联系作者,并在文中注明出处,给出原文链接. 本系列原更新于作者的github博客,这里给出链接. 第1章开始正式进入Unity Shader的学习. 什么是Shader 本 ...
- PHP保留两位小数并且四舍五入及不四舍五入的方法
php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数并且不四舍五入 $num = ...
- [C++ Primer Plus] 零散知识点(一)、输入函数(cin,cin.get,cin.getline等)+string头文件辨析
本文几乎照搬http://www.cnblogs.com/luolizhi/p/5746775.html博客,只修改了一点点.不知道怎么转发过来,尴尬... 学C++的时候,这几个输入函数弄的有点迷糊 ...
- Bugku-CTF之管理员系统+程序员本地网站
Day12 管理员系统 http://123.206.31.85:1003/ flag格式flag{}
- 剑指offer(11)二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 题目分析 首先我们要了解计算机中负数使用补码表示的,原码.补码的概念以及原理可以参考这里,这个题目我们应该从二进制入手,值得 ...
- 【Python】【运算符】
[取模] 所谓取模运算,就是计算两个数相除之后的余数,符号是%.如a % b就是计算a除以b的余数.用数学语言来描述,就是如果存在整数n和m,其中0 <= m < b,使得a = n * ...