当前位置:网站首页>leetcode:78. 子集
leetcode:78. 子集
2022-07-17 00:51:00 【uncle_ll】
78. 子集
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/subsets/
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
提示:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums 中的所有元素 互不相同
解法
- 迭代: 迭代,然后自身遍历加入新的元素
- 回溯:从空列表开始回溯,每次加入新元素,直到当前层遍历到n时候停止当前层回溯
代码实现
迭代
python实现
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = [[]]
for num in nums:
newsets = []
for subset in res:
new_subset = subset + [num]
newsets.append(new_subset)
res.extend(newsets)
return res
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = [[]]
for i in nums:
res = res + [[i] + num for num in res]
return res
c++实现
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
res.push_back({
});
for(int num: nums) {
vector<vector<int>> newsets;
for (auto subset: res) {
subset.push_back(num);
newsets.push_back(subset);
}
res.insert(res.end(), newsets.begin(), newsets.end());
}
return res;
}
};
复杂度分析
- 时间复杂度: O ( n × 2 n ) O(n \times 2 ^ n) O(n×2n)
- 空间复杂度: O ( n ) O(n) O(n)
回溯
python实现
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
n = len(nums)
def helper(i, tmp):
res.append(tmp) # 收集子集,要放在终止添加的上面,否则会漏掉自己
for j in range(i, n): # 当j已经大于数组的长度了,就终止了,for循环本来也结束了,所以不需要终止条件
helper(j+1, tmp+[nums[j]]) # 回溯
helper(0, [])
return res
c++实现
class Solution {
vector<vector<int>> res;
public:
void helper(int i, int n, vector<int> tmp, const vector<int>& nums) {
res.push_back(tmp);
for(int j=i; j<n; j++) {
tmp.push_back(nums[j]);
helper(j+1, n, tmp, nums);
tmp.pop_back();
}
}
vector<vector<int>> subsets(vector<int>& nums) {
int n = nums.size();
helper(0, n, {
}, nums);
return res;
}
};
复杂度分析
- 时间复杂度: O ( n × 2 n ) O(n \times 2 ^ n) O(n×2n)
- 空间复杂度: O ( n ) O(n) O(n)
参考
边栏推荐
- Ncnn allocator memory allocator
- MySQL optimized index
- Go language realizes sending SMS verification code and logging in
- 关于XML文件(六)-与JSON的区别
- 04_ Service registration Eureka
- Tools and methods - Excel plug-in xltools
- Rtx3090 installing pytorch3d
- [MCU simulation] (VIII) instruction system - data transmission instruction
- 【单片机仿真】(二)keil 安装教程
- GraphQL初识
猜你喜欢

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?

Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2

zsh: command not found: mysql

JPA初识(ORM思想、JPA的基本操作)

自动装配 & 集合注入

About XML file (VI) - the difference between JSON and XML file

数据源对象管理(第三方对象资源) & 加载properties文件

一个优酷VIP会员帐号可以几个人用的设备同时登录如何共享多人使用优酷会员账号?

Rhce8 Learning Guide Chapter 1 installing rhel8.4

Graphql first acquaintance
随机推荐
About XML file (VI) - the difference between JSON and XML file
[template record] string hash to judge palindrome string
Shell script receives and returns parameters
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
重写equals为什么要重写hashcode
JDBC连接Mysql数据库
Win10 onedrive failure reinstallation
Zabbix6.0 monitoring vcenter7.0
The place where the dream begins ---- first knowing C language
[MCU simulation] (II) keil installation tutorial
关于XML文件(六)-与JSON的区别
ES6 learning notes - brother Ma at station B
Wechat applet
Net SNMP related commands
[MCU simulation] (XIX) introduction to assembly, assembly instructions, pseudo instructions
05_ Service call ribbon
In depth understanding of machine learning - unbalanced learning: sample sampling technology - [smote sampling method and borderline smote sampling method of manual sampling technology]
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
04_ Service registration Eureka
Es6 notes d'étude - station B Xiao Ma Ge