当前位置:网站首页>Leetcode-78- subset (medium)
Leetcode-78- subset (medium)
2022-06-13 01:03:00 【Didi dada】
78. A subset of ( secondary )
Give you an array of integers nums , Elements in an array Different from each other . Returns all possible subsets of the array ( Power set ).
Solution set You can't Contains a subset of repetitions . You can press In any order Returns the solution set .
Example 1:
Input :nums = [1,2,3]
Output :[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input :nums = [0]
Output :[[],[0]]
Ideas :
- An operation
- backtracking
- iteration
- It can be distinguished according to whether each element is in or not in two states
1、C++ An operation
nums Contained in the n Elements , share 2 n 2^n 2n A subset of . Each element has only two states in the subset , namely : There is 、 non-existent . You can use 0、1 Express ,0 Does not exist ,1 Indicates presence . And just n A binary number of bits can represent 2 n 2^n 2n A decimal number . namely : For each decimal number 2 The number in hexadecimal numbers i Position as 1 It means nums[1], In this subset .
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int n =nums.size();
int subset_num = pow(2,n);
vector<vector<int>> res(subset_num);
for(int i=0;i < subset_num;i++){
for(int j=0;j<n;j++){
if((i>>j)&1)
res[i].emplace_back(nums[j]);
}
}
return res;
}
};
2、python Iterative method — A very unique way of writing
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = [[]]
for each in nums:
res = res + [[each] + num for num in res]
return res
3、C++__ backtracking
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-NpHqHmAb-1626535323077)(C:\Users\36531\AppData\Roaming\Typora\typora-user-images\image-20210606201308483.png)]
class Solution {
public:
vector<vector<int>> res;
vector<int> t;
vector<vector<int>> subsets(vector<int>& nums) {
int n =nums.size();
if(n==0)
return {
{}};
dfs(0,n,nums);
return res;
}
void dfs(int index,int& n,vector<int>& nums){
if(index==n){
res.emplace_back(t);
return ;
}
t.emplace_back(nums[index]);
dfs(index+1,n,nums);
t.pop_back();
dfs(index+1,n,nums);
}
};
边栏推荐
- The grass is bearing seeds
- Five classic articles worth reading
- Rotating camera
- Leetcode-17- letter combination of phone number (medium)
- Jenkins持续集成操作
- Kotlin coroutine suspend function suspend keyword
- Mysql database password modification
- How to solve the duplication problem when MySQL inserts data in batches?
- .net core 抛异常对性能影响的求证之路
- pytorch是什么?解释pytorch的基本概念
猜你喜欢

STM32 USB Basics

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators

Binary tree -- using hierarchical sequence and middle sequence to determine a tree
![[JS component] calendar](/img/20/71bb0c59da29b3cd3418e38cca39c0.jpg)
[JS component] calendar

Liu Hui and introduction to nine chapter arithmetic and island arithmetic

pytorch和tensorflow有什么区别?

Antdpro - protable realizes the linkage effect of two selection boxes
![[latex] insérer une image](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insérer une image

Aof persistence

Five classic articles worth reading
随机推荐
Undirected graph -- computing the degree of a node in compressed storage
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
3623. Merge two ordered arrays
[Latex] 插入圖片
[virtual machine] notes on virtual machine environment problems
304. Merge two ordered arrays
Status of the thread
Common skills for quantitative investment - drawing 3: drawing the golden section line
MySQL performance analysis - explain
MySQL exception: com mysql. jdbc. PacketTooBigException: Packet for query is too large(4223215 > 4194304)
【北亚服务器数据恢复】虚拟机文件丢失导致Hyper-V服务瘫痪的数据恢复案例
Et5.0 value type generation
Traditional machine learning classification model predicts the rise and fall of stock prices
.net core 抛异常对性能影响的求证之路
(01). Net Maui actual construction project
Hard (magnetic) disk (I)
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
[JS] battle chess
Three threads print digital demo alternately