8.5—暴力枚举法—Combinations
来源:互联网 发布:linux下的下载工具 编辑:程序博客网 时间:2024/05/19 18:46
描述
Given two integers n and k, return all possible combinations of k numbers out of 1...n.
For example, If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
Given two integers n and k, return all possible combinations of k numbers out of 1...n.
For example, If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
#include<iostream>#include<algorithm>#include<vector>using namespace std;bool comp(const vector<int> &a, const vector<int> &b){return a.size() < b.size();}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;}vector<vector<int>>Combination(vector<int>data,int k){vector<vector<int>> res = Subsets(data);sort(res.begin(), res.end(), comp);vector<vector<int>> result;for (int i = 0; i < res.size(); i++){if (res[i].size() == k)result.push_back(res[i]);}return result;}int main(){const int n = 4;int a[n];for (int i = 0; i < n; i++)a[i] = i + 1;vector<int> data(begin(a), end(a));//===int k = 2;vector<vector<int>> res =Combination(data,k);for (int i = 0; i < res.size(); i++){for (int j = 0; j < res[i].size(); j++)cout << res[i][j] << " ";cout << endl;}}
阅读全文
0 0
- 8.5—暴力枚举法—Combinations
- 8.6—暴力枚举法—Letter Combinations of a Phone Number
- leetcode:暴力枚举法之Combinations
- 8.1—暴力枚举法—Subsets
- 8.3—暴力枚举法—Permutations
- 暴力法求解——简单枚举(除法)
- 8.2—暴力枚举法—Subsets II
- 8.4—暴力枚举法—Permutations II
- hdu——4462(暴力枚举)
- 暴力枚举法总结
- 暴力枚举法总结
- 暴力枚举法专题
- 暴力求解-枚举法
- 暴力枚举法总结
- poj 3080 暴力法 KMP+暴力枚举
- CF——#142div2 C(暴力枚举)
- HDU5610——暴力枚举(可减少遍历次数)
- HDU 4445——Crazy Tank(数学题,暴力枚举)
- 8.3—暴力枚举法—Permutations
- 8.4—暴力枚举法—Permutations II
- DirectX12笔记 初始化
- 系统设计师必备技能与工具有哪些?
- easyui复选框树动态加载后台数据,实现自动选中数据库中数据。后台语言是.NET
- 8.5—暴力枚举法—Combinations
- 建立多线程的两种方法
- select poll 与epoll模型的总结
- 8.6—暴力枚举法—Letter Combinations of a Phone Number
- Android Camera结合Zxing优化扫码
- 算法编程题-判断一颗树中是否有与另一棵树拓扑结构完全相同的子树
- 使用Aspose.Cells实现后台输出Excel报表(C#)
- java反射的field.get(null)
- 11.1—分治法—Pow(x,n)