LeetCode 46 - Permutations
2016-03-04 17:11
357 查看
Permutations
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].
My Code
class Solution {public:
void doPermute(vector<vector<int> >& res, vector<int> r, vector<int> v)
{
int size = v.size();
if (size == 1)
{
r.push_back(v[0]);
res.push_back(r);
return;
}
for (int i = 0; i < size; i++)
{
vector<int> rr(r);
vector<int> vv(v);
rr.push_back(v[i]);
vv.erase(vv.begin() + i);
doPermute(res, rr, vv);
}
}
vector<vector<int> > permute(vector<int>& nums) {
vector<vector<int> > res;
vector<int> r;
vector<int> v(nums);
doPermute(res, r, v);
return res;
}
};Runtime: 20 ms
相关文章推荐
- iOS7以后,我们如何统计和跟踪用户
- 单链表反转问题
- OCTEON与DPDK
- 决策树模型组合之随机森林(Random Forest)
- 因为数据库正在使用,所以无法获得对数据库的独占访问权
- java学习---访问权限控制
- The entitlements specified in your application’s Code Signing Entitlements file do not match those s
- MPP VS HADOOP场景分析
- 搜狗语音识别之配置
- Git与Github的使用(ubuntu)
- 素数求和问题(素数打表)
- HBase总结:LSM理解
- MATLAB feof和~feof函数用法
- jvm启动一段时间后无法使用的原因
- Nt函数原型头文件
- Java定位CPU使用高问题--转载
- java string与byte互转
- java集合类——Stack类
- tomcat 日志切割shell脚本
- Android平台arm64 ptrace hook bridge_code debug