8.4—暴力枚举法—Permutations II
来源:互联网 发布:linux下的下载工具 编辑:程序博客网 时间:2024/06/01 16:49
描述
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
#include<iostream>#include<vector>#include<iomanip>#include<algorithm>using namespace std;vector<vector<int>> permutations;void swap(int &a, int &b){int temp = a;a = b;b = temp;}void Permutations(int a[], int from, int to){if (a == NULL)return;if (from == to){vector<int> path;for (int i = 0; i <= to; i++){path.push_back(a[i]);}permutations.push_back(path);}else{for (int i = from; i <= to; i++){swap(a[from], a[i]);Permutations(a, from + 1, to);swap(a[from], a[i]);}}}int main(){const int n = 3;int a[n] = { 1, 2, 1 };int from = 0;int to = n - 1;//===Permutations(a, from, to);sort(permutations.begin(), permutations.end());auto it = unique(permutations.begin(), permutations.end());permutations.resize(distance(permutations.begin(), it));//===for (int i = 0; i < permutations.size(); i++){for (int j = 0; j < permutations[i].size(); j++)cout << permutations[i][j] << " ";cout << endl;}}
阅读全文
0 0
- 8.4—暴力枚举法—Permutations II
- 8.3—暴力枚举法—Permutations
- 8.2—暴力枚举法—Subsets II
- leetcode:暴力枚举法之Permutations
- leetcode:暴力枚举法之Subsets II
- LeetCode——Permutations II
- LeetCode47——Permutations II
- 8.1—暴力枚举法—Subsets
- 8.5—暴力枚举法—Combinations
- leetcode_Permutations II _hard_暴力枚举法--回溯法
- 8.4 Permutations II
- leetcode 047 —— Permutations II
- 算法系列——Permutations II
- 【回溯法】Permutations II
- 暴力法求解——简单枚举(除法)
- hdu——4462(暴力枚举)
- 暴力枚举法总结
- 暴力枚举法总结
- 8.2—暴力枚举法—Subsets II
- 微信小程序实现列表上拉加载,自动适应屏幕
- Kotlin 设计模式-策略
- asd
- 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优化扫码