46. Permutations
2016-05-09 19:12
260 查看
Given a collection of distinct numbers, return all possible permutations.
For example,
the following permutations:
and
【思路】求给定向量数组所有元素的全排列问题。
n个元素有n!种全排列,而STL底层文件<
algorithm >中有关于排列元素的标准算法:
该函数会改变区间[beg , end)内的元素次序,使它们符合“下一个排列次序”
该函数会改变区间[beg , end)内的元素次序,使它们符合“上一个排列次序”
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].
【思路】求给定向量数组所有元素的全排列问题。
n个元素有n!种全排列,而STL底层文件<
algorithm >中有关于排列元素的标准算法:
bool next_permutation(BidirectionalIterator beg , BidirectionalIterator end);
该函数会改变区间[beg , end)内的元素次序,使它们符合“下一个排列次序”
bool prev_permutation(BidirectionalIterator beg , BidirectionalIterator end);
该函数会改变区间[beg , end)内的元素次序,使它们符合“上一个排列次序”
class Solution { public: vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> re; if(nums.size()==0) return re; sort(nums.begin(), nums.end()); re.push_back(nums); while(next_permutation(nums.begin(),nums.end())) { re.push_back(nums); } return re; } };
相关文章推荐
- Ligerui树操作
- 通过反射获取成员变量并使用
- 错误1500。另一个程序正在进行。在继续此安装之前您必须完成另外安装。
- Android audio HAL详解(一)
- 数值的整数次方――11
- 打赏功能的实现
- 项目中的小问题大麻烦
- django简明操作(自强学堂)
- TLB
- PAT乙级—1026. 程序运行时间(15)-native
- 云计算平台简介(App Engine)
- matlab文件数据处理
- ubuntu中安装iso文件
- nexus-2.13.0-01.war
- Notification通知的详解
- 最常用的 Eclipse 快捷键总结
- 集合框架--Map集合之记录字母次数练习
- 数据结构:队列
- 《Day1 获取不在同一页面的样式属性》
- ubuntu下安装matlab2015b