[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 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
随机推荐
- qt 安装包生成2
使用Qt Installer Framework制作安装包 2018年07月01日 03:45:37 大黄老鼠 阅读数:878 标签: qt更多 个人分类: Qt 版权声明:本文为博主原创文章,未 ...
- Scrapy框架的学习(6.item介绍以及items的使用(提前定义好字段名))转载https://blog.csdn.net/wei18791957243/article/details/86259688
在Scrapy框架中的items.py的作用 1.可以预先定义好要爬取的字段 items.py import scrapy class TencentItem(scrapy.I ...
- 笔记-ASP.NET WebApi
本文是针对ASP.NET WepApi 2 的笔记. Web API 可返回的结果: 1.void 2.HttpResponseMessage 3.IHttpActionResult 4.其他类型 返 ...
- iterator与const_iterator
iterator与const_iterator 所有的标准库容器都定义了相应的迭代器类型.迭代器对所有的容器都适用,现代 C++ 程序更倾向于使用迭代器而不是下标操作访问容器元素. 1.iterato ...
- 0001-20180421-自动化第一章-python基础学习笔记
======================学习python==================介绍: python种类: cpython(*),jpython,ironpython,rubypyth ...
- 【winform】listbox 列表
一.Item 一个ListBox是由一个个的Item项组成的 1.向下添加Item lstResult.Items.Add("***" + PostWebRequest(cbxUr ...
- jQuery 自执行函数
jQuery 自执行函数 // 为了避免三方名冲突可将全局变量封装在自执行函数内 (function (arg) { var status = 1; arg.extend({ 'xsk': funct ...
- [c/c++] programming之路(12)、循环结构
一.求2n #include<stdio.h> void main(){ ; ; while(n--){ s*=; printf("%d,%d\n",s,n); } g ...
- 【Python游戏编程01--初步认识pygame】
一.pygame简介 Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发.允许你在 Python 程序中创建功能丰富的游戏和多媒体程序,Pygame 是一个高 ...
- 通过WireShark抓取iPhone联网数据方法
通过WireShark抓取iPhone联网数据方法 命令行 rvictl -s <UDID> 然后再wireshark选择rvi0进行抓包即可 抓包完后,移除用命令 rvictl -x & ...