8.1—暴力枚举法—Subsets
来源:互联网 发布:激光洗眉机网络假货 编辑:程序博客网 时间:2024/05/29 10:33
描述
Given a set of distinct integers, S, 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 S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Given a set of distinct integers, S, 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 S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
#include<iostream>#include<algorithm>#include<vector>using namespace std;vector<vector<int>> Subsets(vector<int> data){vector<vector<int>> result;if (data.size() <= 0)return result;int length = data.size();int num = pow(2, length);int num1 = pow(2, length - 1);sort(data.begin(), data.end());for (int i = 0; i < num; i++){vector<int> path;if (i == 0){result.push_back(path);continue;}for (int j = 0; j < length;j++){int temp = num1;temp = temp >>= j;if (i&temp){path.push_back(data[j]);}}result.push_back(path);}return result;}int main(){int a[4] = { 1, 8,2, 3 };vector<int> data(begin(a), end(a));vector<vector<int>> res = Subsets(data);for (int i = 0; i < res.size(); i++){if (res[i].size() == 0)cout << "[]";for (int j = 0; j < res[i].size(); j++)cout << res[i][j] << " ";cout << endl;}}
阅读全文
0 0
- 8.1—暴力枚举法—Subsets
- 8.2—暴力枚举法—Subsets II
- leetcode:暴力枚举法之Subsets
- leetcode:暴力枚举法之Subsets II
- 8.3—暴力枚举法—Permutations
- 8.5—暴力枚举法—Combinations
- LeetCode 之 Subsets(图和暴力枚举)
- 暴力法求解——简单枚举(除法)
- 8.4—暴力枚举法—Permutations II
- hdu——4462(暴力枚举)
- 暴力枚举法总结
- 暴力枚举法总结
- 暴力枚举法专题
- 暴力求解-枚举法
- 暴力枚举法总结
- poj 3080 暴力法 KMP+暴力枚举
- 8.6—暴力枚举法—Letter Combinations of a Phone Number
- CF——#142div2 C(暴力枚举)
- 框架就快开源了
- 使用若要打开fc-list,您需要安装X11
- wamp环境配置phpstorm的断点调试功能。和打开debug会出现waiting for incoming connetcion with ide key 17173的问题
- Struts2-01
- 深入理解Java:注解(Annotation)--注解处理器
- 8.1—暴力枚举法—Subsets
- unity如何调试dll库文件
- Android studio使用selector时出现Element selector must be declared的错误解决
- 信息系统设计师不同研发阶段的工作内容有哪些?
- fragment常用方式总结
- 8.2—暴力枚举法—Subsets II
- 微信小程序实现列表上拉加载,自动适应屏幕
- Kotlin 设计模式-策略
- asd