首页 > 其他分享 >lintcode:Subsets

lintcode:Subsets

时间:2022-12-01 19:04:29浏览次数:39  
标签:sub Subsets nums int res lintcode vector choosed


Given a set of distinct integers, return all possible subsets.

Challenge
Can you do it in both recursively and iteratively?

1.18s

class Solution {
public:
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
vector<vector<int> > subsets(vector<int> &nums) {
// write your code here
int len=nums.size();
vector<vector<int> >res;

int sum=1<<len;
for(int i=0;i<sum;i++){
vector<int> sub;
for(int j=0;j<len;j++){
if(i&(1<<j)){
sub.push_back(nums[len-1-j]);
}
}
reverse(sub.begin(),sub.end());
res.push_back(sub);
}

return res;
}
};

2.21s

class Solution {
public:
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
vector<vector<int> > res;

void dfs(vector<int> &nums, vector<bool> &choosed, int cur){
if(cur==nums.size()){
vector<int> sub;
for(int i=0;i<choosed.size();i++){
if(choosed[i]){
sub.push_back(nums[i]);
}
}
res.push_back(sub);
return;
}

choosed[cur]=true;
dfs(nums,choosed,cur+1);
choosed[cur]=false;
dfs(nums,choosed,cur+1);
}

vector<vector<int> > subsets(vector<int> &nums) {
// write your code here
vector<bool> choosed(nums.size(),false);

dfs(nums,choosed,0);

return res;
}
};


标签:sub,Subsets,nums,int,res,lintcode,vector,choosed
From: https://blog.51cto.com/u_15899184/5903611

相关文章

  • lintcode: Permutations II
    Givenalistofnumberswithduplicatenumberinit.Findalluniquepermutations.可以见我的博文​​全排列实现​​classSolution{public:/***@paramnu......
  • lintcode: Subsets II
    Givenalistofnumbersthatmayhasduplicatenumbers,returnallpossiblesubsets1.先排序;再按求Subsets一样的做法,只是添加前检查是否已经存在。耗时171mscla......
  • lintcode:Subarray Sum Closest
    Givenanintegerarray,findasubarraywithsumclosesttozero.Returntheindexesofthefirstnumberandlastnumber.ExampleGiven[-3,1,1,-3,5],retur......
  • lintcode: Sqrt(x)
    Implementintsqrt(intx).Computeandreturnthesquarerootofx.Examplesqrt(3)=1sqrt(4)=2sqrt(5)=2sqrt(10)=3ChallengeO(log(x))求非线性方程的解可以......
  • SP11469 SUBSET - Balanced Cow Subsets Sol
    考虑枚举\(3^n\)种情况,用三进制数表示。对于每一位,\(0\)表示不放,\(1\)表示放入第一个集合,\(2\)表示放入第二个集合。这样显然会TLE。考虑meetinthemiddle。考......
  • 78. Subsets
    样例输入{1,8,5,4}输出[[],[1],[1,4],[1,4,5],[1,4,5,8],[1,4,8],[1,5],[1,5,8],[1,8],[4],[4,5],[4,5,8],[4,8],[5],[5,8],[8]]publi......
  • LeetCode 90. Subsets II
    ​​题目​​dfsclassSolution{public:vector<vector<int>>ans;vector<int>res;vector<vector<int>>subsetsWithDup(vector<int>&nums){......
  • 洛谷 P3067 [USACO12OPEN]Balanced Cow Subsets G 折半搜索
    题目https://www.luogu.com.cn/problem/P3067思路考虑折半搜索,第一个dfs对[1,n/2]的数进行分组,+代表第一组,-代表第二组,并计算两组总和的情况方案数\(a_i\)。第二个dfs......
  • Problem P32. [算法课分支限界法]Partition to K Equal Sum Subsets
    纯暴力遍历+剪枝,将任务看出有k个桶,要将每个桶都刚刚好装满。#include<iostream>#include<bits/stdc++.h>#include<cstdio>#include<string>usingnamespacestd;......
  • CF895C Square Subsets
    CF895CSquareSubsets注意到平方数要求每个质因数的幂次均为偶数。由于\(70\)以内仅有\(19\)个质因数,考虑将每个\(a_i\)按照每个质因数的奇偶性分解为\(01\)串......