Permutations
来源:互联网 发布:产品价格查询软件 编辑:程序博客网 时间:2024/05/03 06:24
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: void heapKeep(vector<int> &num,int i,int len){ int max=i; int le = 2*i; int ri = 2*i+1; if (le<len && num[le]>num[max]) max = le; if (ri<len && num[ri]>num[max]) max = ri; if (max != i) { swap(num[i],num[max]); heapKeep(num,max,len); } } void heapBuild(vector<int> &num){ int len = num.size(); for (int i=len/2; i>=0; --i){ heapKeep(num,i,len); } } void heapSort(vector<int> &num){ int len = num.size(); heapBuild(num); for (int i=len-1; i>0; --i){ swap(num[0],num[i]); heapKeep(num,0,i); } } void reserveVec(vector<int> &num,int begin,int end){ while (begin<end) { swap (num[begin++],num[end--]); } } vector<vector<int> > permute(vector<int> &num) { //从小到大 int len = num.size(); vector<vector<int>> vec; if (len==0) return vec; heapSort(num); while (true) { int index =len -1; int littlebig = index; vec.push_back(num); while (index>=1 && num[index-1]>num[index]) { index--; } if (index==0) return vec; while (num[littlebig]<=num[index-1]){ littlebig--; } swap(num[littlebig],num[index-1]); reserveVec(num,index,len-1); } return vec; }};
0 0
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- 百万用户级游戏服务器架构设计(2)
- 性能优化(转)
- redis 札记 -- phpredis 中文说明
- ora-01033:oracle initialization or shutdown in progress 解决方法
- Java Web项目开发到底需要掌握哪些技术?
- Permutations
- OCP-1Z0-053-200题-81题-503
- DELPHI中怎样将指定文件路径加到Library中去?
- 串结构练习--字符串匹配
- 年前整理的Js规范
- SimpleDateFormat 函数语法
- android开发学习笔记
- 关键字-friend
- OnDraw\OnPaint区别