当前位置:网站首页>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);
}
};
边栏推荐
猜你喜欢

軟件測試的幾種分類,一看就明了

With a market value of more than trillion yuan and a sales volume of more than 100000 yuan for three consecutive months, will BYD become the strongest domestic brand?

Status of the thread
![[Latex] 插入图片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入图片

Opencv desaturation

什么是 Meebits?一个简短的解释

在国企做软件测试工程师是一种什么样的体验:每天过的像打仗一样

深度学习模型剪枝

Breadth first search for node editor runtime traversal

Leetcode-11- container with the most water (medium)
随机推荐
Breadth first search for node editor runtime traversal
Cards are unpredictable
Minimum spanning tree problem
What is dummy change?
[JS component library] drag sorting component
今日在家休息
[JS component] customize the right-click menu
什么是 dummy change?
Unity calls alertdialog
Et5.0 value type generation
什么是 Meebits?一个简短的解释
蓝桥杯单片机第七届决赛
ArrayList underlying source code
How many rounds of deep learning training? How many iterations?
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of vrsi, bbiboll, WR, bias and RSI indicators
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
How many steps are appropriate for each cycle of deep learning?
The grass is bearing seeds
Canvas airplane game
Wal mechanism of MySQL