leetcode 46. Permutations
来源:互联网 发布:手机摄影软件 编辑:程序博客网 时间:2024/05/16 06:22
Given a collection of distinct numbers, return all possible permutations.
For example,[1,2,3]
have the following permutations:[1,2,3]
, [1,3,2]
, [2,1,3]
, [2,3,1]
, [3,1,2]
, and [3,2,1]
.
class Solution {public:vector<vector<int>> permute(vector<int>& nums) {vector<vector<int>>re;if (nums.empty())return re;if (nums.size() == 1){re.push_back(nums);return re;}map<vector<int>, vector<int>>candi;vector<int>aa; candi[aa] = nums;int k = 0;while (true){map<vector<int>, vector<int>>newcandi;for (map<vector<int>, vector<int>>::iterator it = candi.begin(); it != candi.end(); it++){for (int i = 0; i < it->second.size(); i++){vector<int>aa = it->first;aa.push_back(it->second[i]);if (it->second.size() == 2){aa.push_back(it->second[(1 + i) % 2]);re.push_back(aa);}else{vector<int>bb = it->second;bb.erase(bb.begin() + i, bb.begin() + i + 1);newcandi[aa] = bb;}}}if (k == nums.size() - 2)return re;k++;candi = newcandi;}}};
accepted
0 0
- [LeetCode]46.Permutations
- LeetCode 46.Permutations
- 46. Permutations Leetcode Python
- LeetCode --- 46. Permutations
- [Leetcode] 46. Permutations
- [leetcode] 46.Permutations
- LeetCode 46. Permutations DFS
- 46. Permutations LeetCode
- Leetcode 46. Permutations
- leetcode 46. Permutations
- LeetCode *** 46. Permutations
- LeetCode 46. Permutations
- leetcode 46. Permutations
- LeetCode - 46. Permutations
- 【leetcode】46. Permutations
- leetcode 46. Permutations
- [LeetCode] 46. Permutations
- leetcode 46. Permutations
- 火车进站
- 在JLabel上显示图片,并且图片自适应JLabel的大小
- QT 的单选按钮使用方法
- VS2010测试功能之旅:编码的UI测试(1)
- java Object类及其常用方法
- leetcode 46. Permutations
- Codeforces 215A A.Sereja and Coat Rack
- mongoDB——read&write操作
- 杭电5640 King's Cake
- python勿使用mutable值作为默认参数
- notePad++快捷键用法
- centos忘记root密码,重新设置的方法
- leetcode_023 Merge k Sorted Lists
- java项目——提升网站性能之java模板引擎beetl