[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 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
随机推荐
- Python str byte 互相转换
- 为VisualStudio2017添加bits/stdc++.h
在算法编程中经常有人只写一个头文件"bits/stdc++.h" 其实这个是很多头文件的集合,写了它后相当于包含了所有常用的C++头文件,可是需要注意的是并不是所有的OJ系统都支持 ...
- day04 一个简单的代码优化案例
import random punches = ['石头','剪刀','布'] computer_choice = random.choice(punches) user_choice = input ...
- JavaWeb-----实现第一个Servlet程序
1.Servlet简介 Servlet是在服务器端运行的一个小程序,实际上一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问的这个驻留在服务器内 存里的servl ...
- MySQL数据的导出和导入
MySQL环境变量设置,将%MySQL_HOME%下的MySQL Server 5.1/bin放到Path下. MySQL的mysqldump工具,基本用法是: shell> mysqldu ...
- Linux 基础内容
1.linux版本有:redhat(收费),centos,ubuntu,suse(开发使用) 2./目录下的:etc配置文件目录,media挂载点,opt第三方安装目录,boot启动文件,home家, ...
- Tomcat的overview界面说明
Tomcat的overview界面说明 一.Tomcat的overview界面 双击或者open,进入Tomcat的overview界面, 一般情况workspace的子路径为.metadata.pl ...
- react-router 4.0(四)跳转404
import React from 'react' import ReactDOM from 'react-dom' import { HashRouter, Route, Link, Prompt, ...
- UVA1401 Remember the Word
思路 用trie树优化dp 设f[i]表示到第i个的方案数,则有\(f[i]=\sum_{x}f[i+len[x]]\)(x是s[i,n]的一个前缀),所以需要快速找出所有前缀,用Trie树即可 代码 ...
- P4248 [AHOI2013]差异
思路 SAM 后缀自动机parent树的LCA就是两个子串的最长公共后缀 现在要求LCP 所以把字符串反转一下 然后每个点的贡献就是endpos的大小,dfs一遍求出贡献就可以了 代码 #includ ...