您的位置:首页 > 其它

leetcode 78. Subsets

2016-03-27 14:38 274 查看
Given a set of distinct integers, nums, return all possible subsets.

Note:

Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.

For example,

If nums = 
[1,2,3]
, a solution
is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]


class Solution {
void choose_one(vector<pair<vector<int>, vector<int>>>&candi)
{
vector<pair<vector<int>, vector<int>>>newcandi;
for (int i = 0; i < candi.size(); i++)
{
for (int j = 0; j < candi[i].second.size(); j++)
{
vector<int>bb = candi[i].first;
bb.push_back(candi[i].second[j]);
vector<int>cc = candi[i].second;
cc.erase(cc.begin(), cc.begin() + j + 1);
newcandi.push_back(pair<vector<int>, vector<int>>(bb, cc));
}
}
candi = newcandi;
}
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>>re;
vector<int>aa,a;
re.push_back(aa);
sort(nums.begin(), nums.end());
vector<pair<vector<int>, vector<int>>>candi;
candi.push_back(pair<vector<int>, vector<int>>(a, nums));
int k = 0;
while (k < nums.size())
{
choose_one(candi);
for (int i = 0; i < candi.size(); i++)
re.push_back(candi[i].first);
k++;
}
return re;
}
};

accepted
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: