【leetcode】Permutations
来源:互联网 发布:rpm安装java 编辑:程序博客网 时间:2024/06/03 09:33
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: vector<vector<int> > permute(vector<int>& nums) { vector<vector<int> > result; int N=nums.size(); if(N==1) { result.push_back(nums); } vector<vector<int> > all;//存储除了第一个数外的全排列 vector<int> inner; //number除去一个数之后的全排列 vector<int> temp; //记录inner for(int i=0;i<N;i++) { //所有的数都依次当第一位,然后,剩余的数全排列 inner=nums; inner.erase(inner.begin()+i); all=permute(inner); for(int j=0;j<all.size();j++) { temp=all[j]; temp.insert(temp.begin(),nums[i]); result.push_back(temp); } } return result; }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- Toolkit类
- TcpClient & TcpServer 第一版
- Android 5.0 parseSdkContent failed Could not initialize class android.graphics.Typeface
- java多线程
- mvc理解
- 【leetcode】Permutations
- 我的求职之路2015
- hdu-1078 FatMouse and Cheese (and) 滑雪问题(记忆化搜索+简单dp)
- 如何动态地给vSphere虚拟机模板注入信息
- 并发性:互斥和同步、死锁和饥饿
- Google ProtocolBuffer(PB) 简介和实例2-Wire的使用
- FusionCharts中文消息提示
- 解决Matlab 2012b中无法选择Visual Studio 2012作为编译器的问题
- java io的架构