Permutations
2015-08-01 14:35
369 查看
Permutations
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 permutation(vector<vector<int>> &ret, vector<int> &nums,int pStr,int pbegin) { if(pbegin == nums.size()) ret.push_back(nums); else { for(int i = pbegin;i < nums.size();++i) { int tmp = nums[pbegin]; nums[pbegin] = nums[i]; nums[i] = tmp; permutation(ret,nums,pStr,pbegin+1); tmp = nums[pbegin]; nums[pbegin] = nums[i]; nums[i] = tmp; } } } vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> ret; if(0 == nums.size()) return ret; permutation(ret,nums,0,0); return ret; } };
相关文章推荐
- A股借壳上市?暂时没戏
- Hibernate内置映射
- 机试算法讲解: 第40题 暴力搜索之百鸡问题
- 面向对象概述
- 对LessonFour的再一遍
- ZOJ 3299 Fall the Brick(线段树区间更新)
- 正则表达式
- 二叉树的基本操作
- ubuntu 常用命令大全
- Android中BitmapFactory.Options详解
- Selenium2学习-023-WebUI自动化实战实例-021-获取浏览器显示区域大小,通过 WebDriver 截图功能
- UVa 232 Crossword Answers
- leetcode--Lowest Common Ancestor of a Binary Tree
- 链路层 - SLIP,PPP,
- 链路层 - SLIP,PPP,
- 欢迎使用CSDN-markdown编辑器
- 机试算法讲解: 第39题 DAG之QQ师徒关系
- Linux下Socket编程
- 士兵杀敌(一)
- HDU 5338 ZZX and Permutations 线段树