当前位置:网站首页>力扣78:子集
力扣78:子集
2022-06-25 06:41:00 【瀛台夜雪】
力扣78:子集
题目描述
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
输入输出示例
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
输入:nums = [0]
输出:[[],[0]]
算法一,使用字典进行回溯
vector<vector<int>>subsets(vector<int>&nums)
{
//设定判断数组
vector<bool>vaild(nums.size(),false);
//设定保存数组
vector<vector<int>>res;
//设置遍历的深度
int depth=0;
//数组的长度
int length=nums.size();
getSubsets(nums,length,depth,vaild,res);
return res;
}
//建立函数实现迭代的功能,根据vaild函数值的变化实现
void getSubsets(vector<int>nums,int length,int depth,vector<bool>&vaild,vector<vector<int>>&res)
{
vector<int>path;
if(depth==length)
{
for(int i=0;i<length;i++)
{
if(vaild[i]==true)
{
path.push_back(nums[i]);
}
}
res.push_back(path);
}
else{
//将当前的vaild值的两个分成true和false,不断进行下探
vaild[depth]=true;
getSubsets(nums,length,depth+1,vaild,res);
vaild[depth]=false;
getSubsets(nums,length,depth+1,vaild,res);
}
}
算法二,使用回溯
//使用迭代的思想
vector<vector<int>>subsets2(vector<int>&nums)
{
vector<vector<int>>res;
vector<int>path;
int start=0;
backTrack(nums,path,start,res);
return res;
}
void backTrack(vector<int>nums,vector<int>&path,int start,vector<vector<int>>&res)
{
res.push_back(path);
for(int i=start;i<nums.size();i++)
{
path.push_back(nums[i]);
backTrack(nums,path,i+1,res);
path.pop_back();
}
}
算法3:使用动态规划
//使用动态规划的思想
vector<vector<int>>subsets3(vector<int>&nums)
{
vector<vector<int>>res;
vector<int>path;
//将空集先加入到数组中
res.push_back(path);
for(int i=0;i<nums.size();i++)
{
int size=res.size();
//往上一个子集中添加
for(int j=0;j<size;j++)
{
vector<int>newList=res[j];
newList.push_back(nums[i]);
res.push_back(newList);
}
}
return res;
}
边栏推荐
- CPDA | how to start the growth path of data analysts?
- 【Qt】快捷键
- How to use printf of 51 single chip microcomputer
- This year, I graduated
- Four software 2021-10-14 suitable for beginners to draw PCB
- Storage of Galileo broadcast ephemeris in rtklib-b33
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
- OAuth 2.0一键登录那些事
- Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
- JDBC-DAO层实现
猜你喜欢

el-input实现尾部加字

My debut is finished!

Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute

Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"

Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)

Misunderstanding of switching triode

Fairmot yolov5s to onnx

Construction of occupancy grid map

Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes

Manufacturing process of PCB 2021-10-11
随机推荐
smartBugs安装小问题总结
ts环境搭建
三年营收连续下滑,天地壹号困在醋饮料里
Hisilicon 3559 sample parsing: Vio
点云智绘在智慧工地中的应用
微信小程序入门记录
Fairmot yolov5s to onnx
【QT】qtcreator便捷快捷键以及QML介绍
数据可视化没有重点怎么办?
Sichuan earth microelectronics 8-channel isolated digital input receiver
C Getting Started tutorial
函数模板_类模板
Construction of occupancy grid map
VectorDraw Web Library 10.10
Domestic MCU perfectly replaces STM chip model of Italy France
Summary of small problems in smartbugs installation
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
Chuantu microelectronics ca-if1051 can-fd transceiver
STL教程4-输入输出流和对象序列化
力扣76题,最小覆盖字串