Permutations -- leetcode
来源:互联网 发布:mac 之间airdrop怎么用 编辑:程序博客网 时间:2024/06/03 19:09
Given a collection of 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> &num) { vector<vector<int> > ans; helper(ans, 0, num); return ans; } void helper(vector<vector<int> > &ans, size_t start, vector<int> &num) { if (start+1 >= num.size()) return ans.push_back(num); for (size_t i=start; i<num.size(); i++) { swap(num[start], num[i]); helper(ans, start+1, num); swap(num[start], num[i]); } }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- Android控件:textView
- Android JNI知识总结
- Windows共享文件夹的权限设定
- POJ 2409
- C++实现视频流转换为图片
- Permutations -- leetcode
- Java 8简明教程
- iOS UIImage的剪裁、合并实现
- 彻底搞懂C语言指针
- CODEVS 1204 寻找子串位置
- 溱湖拓展训练基地-绿岛度假村拓展培训中心-溱湖湿地公园拓展培训
- 让文本输入框充满屏幕的的宽度
- bootstrap 导航条 组件排列
- 浅谈jms之中间件(mom)activeMQ的安装和部署