Permutations
来源:互联网 发布:麒麟与ubuntu的区别 编辑:程序博客网 时间:2024/06/11 14:20
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]
.
首先排序
1.先从右到左,找到第一个违反递减规律的数num[i]
2.再从右到i+1 找到第一个比num[i]大的数字num[j]
3.交换num[i] 与num[j]
4.对i+1到num.end() 排序
swap+sort 策略
class Solution {public: bool next(vector<int>& num){ int i=num.size()-2,j=num.size()-1; while(i>=0&&num[i]>=num[i+1]) i--; if(i<0) return false; while(j>i&&num[j]<=num[i]) j--; swap(num[i],num[j]); if(i+1<num.size()-1) sort(num.begin()+i+1,num.end()); return true; } vector<vector<int> > permute(vector<int> &num) { sort(num.begin(),num.end()); vector<vector<int>> result; do{ result.push_back(num); }while(next(num)); return result; }};
0 0
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- 共有继承 私有继承 保护继承 派生类的三种继承方式小结
- Hibernate格式化sql语句实体类 BasicFormatterImpl 源码
- Android 短信收件人自动匹配与用户输入的显示不一致
- Vim简明教程
- hdoj 1518 Square 【dfs】
- Permutations
- gcc 中参数-ftest-coverage -fprofile-arcs 使用
- Android SMS 从备份恢复后,会把手机原有信息删除掉
- iPhone文件读写系统操作
- 【note】stm32 keilMDK出现warning: function XX declared implicitly
- Console程序中取得窗口句柄
- php的memcached扩展
- 大中型网站使用图片域名的必要性原因(firebug Blocking)
- JavaScript AMD规范简介(一)