LeetCode *** 46. Permutations
2016-04-12 20:32
176 查看
题目:
Given a collection of distinct numbers, return all possible permutations.
For example,
分析:
dfs搜索即可。
代码:
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].
分析:
dfs搜索即可。
代码:
class Solution { public: vector<vector<int>> res; vector<vector<int>> permute(vector<int>& nums) { vector<int> tmp; bool *mark=new bool[nums.size()]; for(int i=0;i<nums.size();++i)mark[i]=false; dfs(tmp,nums,mark); return res; } void dfs(vector<int>& tmp,vector<int>& nums,bool*& mark){ if(nums.size()==tmp.size()){ res.push_back(tmp); return; } for(int i=0;i<nums.size();++i){ if(!mark[i]){ mark[i]=true; tmp.push_back(nums[i]); dfs(tmp,nums,mark); tmp.erase(tmp.end()-1); mark[i]=false; } } } };
相关文章推荐
- Error C2662
- nginx学习记录
- Thread.isAlive()
- 第三方框架简介ios
- markdown编辑器使用指导
- c#方法生成mysql if方法(算工作日)
- c++ 设计模式之单件模式
- 广播和Service唤醒被杀死的android app
- window.parent与window.opener的区别与使用
- Laxcus大数据管理系统2.0(3)- 第一章 基础概述 1.2 产品特点
- 你足够了解Context吗?
- PSobj [3] 脱墨喷溅和炫光特效
- android基础---->LruCache的使用及原理
- 倒计时四天——04:垂直直方图
- Java基础知识
- C++实验3—个人所得税计算器
- final和static final之间的区别
- 序列解包 报错原因
- POJ-3373 (DP)
- 使Handler内部类不导致内存泄露的解决方法