当前位置:网站首页>力扣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;
}
边栏推荐
- LeetCode_哈希表_中等_454.四数相加 II
- 单位转换-毫米转像素-像素转毫米
- VectorDraw Web Library 10.10
- [QT] shortcut key
- Intel announced five new technological developments, including quantum computing, neural pseudo computing, machine programming, integrated optoelectronics, and secure computing
- C Getting Started tutorial
- Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
- Collection of common terms and meanings in forestry investigation based on lidar
- GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
- 2265. 统计值等于子树平均值的节点数
猜你喜欢

LeetCode 每日一题——515. 在每个树行中找最大值

Function template_ Class template

Chuantu microelectronics ca-if1051 can-fd transceiver

Application scheme | application of Sichuan earth microelectronics ca-is398x in PLC field

无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略

ts环境搭建

Tupu software digital twin 3D wind farm, offshore wind power of smart wind power

FairMOT yolov5s转onnx

Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks

The method of judging whether triode can amplify AC signal
随机推荐
國外LEAD域名郵箱獲取途徑
el-input实现尾部加字
Pit encountered by pytorch: why can't l1loss decrease during model training?
个人域名和企业域名的区别
My debut is finished!
基于激光雷达的林业调查常用术语及含义锦集
“空间转换”显著提升陡崖点云的地面点提取质量
CGLIB动态代理
[batch dos-cmd command - summary and summary] - add comment command (REM or::)
CPDA|数据分析师成长之路如何起步?
Vscode official configuration synchronization scheme
57. 插入区间
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
Chuantu microelectronics ca-if1051 can-fd transceiver
Modular programming of oled12864 display controlled by single chip microcomputer
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
WinForm实现窗口始终在顶层
Explain distributed raft with dynamic diagram
[batch dos-cmd command - summary and summary] - CMD window setting and operation commands (CD, title, mode, color, pause, CHCP, exit)
C reads XML on the web