LeetCode: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].
思路:将元素一个一个的插入,首先只有一个元素{1},此时,插入之后会的到两个vector<int>,{1,2},{2,1},然后继续插入第三个元素3,会得到{3,1,2},{1,3,2},{1,2,3}和{3,2,1},{2,3,1},{2,1,3}。
依次类推,将所有的元素插入其中。
C++代码实现:
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
if(num.empty())
return vector<vector<int> >();
vector<vector<int> > ret{{num[]}};
size_t i,j,k;
for(i=;i<num.size();i++)
{
vector<int> temp1;
vector<vector<int> > temp2=ret;
ret.clear();
for(j=;j<temp2.size();j++)
{
temp1=temp2[j];
k=;
while((temp1.begin()+k)!=temp1.end())
{
temp1.insert(temp1.begin()+k,num[i]);
ret.push_back(temp1);
temp1=temp2[j];
k++;
}
temp1.push_back(num[i]);
ret.push_back(temp1);
}
}
return ret;
}
};
int main()
{
Solution s;
vector<int> vec={,,,};
vector<vector<int> > result=s.permute(vec);
for(auto a:result)
{
for(auto v:a)
cout<<v<<" ";
cout<<endl;
}
}
运行结果:

方法二,使用回溯的方法。
#include<iostream>
#include<vector>
using namespace std; class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
if(num.empty())
return vector<vector<int> >();
vector<vector<int> > ret{{num[]}};
size_t i,j,k;
for(i=;i<num.size();i++)
{
vector<int> temp1;
vector<vector<int> > temp2=ret;
ret.clear();
for(j=;j<temp2.size();j++)
{
temp1=temp2[j];
k=;
while((temp1.begin()+k)!=temp1.end())
{
temp1.insert(temp1.begin()+k,num[i]);
ret.push_back(temp1);
temp1=temp2[j];
k++;
}
temp1.push_back(num[i]);
ret.push_back(temp1);
}
}
return ret;
}
};
int main()
{
Solution s;
vector<int> vec={,,,};
vector<vector<int> > result=s.permute(vec);
for(auto a:result)
{
for(auto v:a)
cout<<v<<" ";
cout<<endl;
}
}
方法三:利用递归的方法(递归-恢复-递归-恢复)
首先解释下全排列怎么生成的,看懂后代码就好写了。例如123,有6种排列方式,这其中有个规律,用第一个数字从前往后与所有数字交换(包括第一个数字本身),每次交换都确定一个位置。
123——最左边的数字确定了——中间的数字确定了
|——123(交换1与1所得)——132(交换2与3所得)——确定最右边的位置,就剩下一位了,肯定确定了。
|——213(交换1与2所得)——231———————— 同上
|——321(交换1与3所得)——312———————— 同上
去除重复的全排列也很简单,例如 :
数字序列1232,交换1与第一个2,得(2)132,括号里的2固定了,递归处理后面132序列的全排
数字序列还是1232,交换1与最后一个2,得(2)231,括号里的2固定了,递归处理后面的231序列的全排
子序列132与231的全排肯定有重复的,这就是造成重复的原因
实现代码:
class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int> > res;
sort(num.begin(),num.end());
helper(num,,num.size()-,res);
return res;
}
void helper(vector<int> &num,int start,int end,vector<vector<int> > &res)
{
if(start==end)
{
res.push_back(num);
}
else
{
for(int i=start;i<=end;i++)
{
swap(&num[start],&num[i]);
helper(num,start+,end,res);
swap(&num[start],&num[i]);
}
}
}
void swap(int* a,int *b)
{
int tmp=*a;
*a=*b;
*b=tmp;
}
};
LeetCode:Permutations(求全排列)的更多相关文章
- [LeetCode] Permutations II 排列
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode——Permutations
Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...
- PermutationsUnique,求全排列,去重
问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- 求全排列Permutation
是在教材(<计算机算法设计与分析(第4版)>王晓东 编著)上看见的关于求全排列的算法: 我们可以看一下书上怎么写的: #include<bits/stdc++.h> using ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- LeetCode OJ:Permutations(排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)
描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...
随机推荐
- spring mvc easyui tree 异步加载树
使用spring mvc 注解 异步加载一棵树 jsp: <ul id="orgInfoTree"></ul> $(function(){ loadOrgT ...
- Bootstrap_Javascript_弹出框
HTML: <button type="button" class="btn btn-default" data-container="body ...
- BAE 环境下 hibernate annotations 配置
annotations 配置 首先需要加入 hibernate-jpa-2.0-api-1.0.1.Final.jar 和 ejb3-persistence.jar 这两个包 ejb3-persis ...
- centos下Elasticsearch数据迁移与备份
########### ### 共享创建es官方网站就一句话 ######## 1.下载 文件共享 .. rpm -i http://mirror.symnds.com/distributions ...
- linker command failed with exit code 1
这种问题,通常出现在添加第三方库文件或者多人开发时. 这种问题一般是找不到文件而导致的链接错误. 我们可以从如下几个方面着手排查. 1.以如下错误为例,如果是多人开发,你同步完成后发现出现如下的错误. ...
- JS获取select选中的值
var oSel=oFl.getElementsByTagName('select')[0]; oSel.onchange=function(){ var indexselect=oSel.selec ...
- 转:Java架构师与开发者提高效率的10个工具
原文来自于:http://www.importnew.com/14624.html Java受到全球百万计开发者的追捧,已经演变为一门出色的编程语言.最终,这门语言随着技术的变化,不断的被改善以迎合变 ...
- Hadoop 学习笔记 (八) hadoop2.2.0 测试环境部署 及两种启动方式
1基本流程步骤1:准备硬件(linux操作系统)步骤2:准备软件安装包,并安装基础软件(主要是JDK)步骤3:修改配置文件步骤4:分发hadoop步骤5:启动服务步骤6:验证是否启动成功!2硬件配置要 ...
- javascript content
1. Chrome, Mozila, IE 2. jQuery, underscore, zepto 3. underscore 4. Backbone, AngularJS 5. RequireJS ...
- JPA学习笔记
一.JPA基础1.1 JPA基础JPA: java persistence api 支持XML.JDK5.0注解俩种元数据的形式,是SUN公司引入的JPA ORM规范 元数据:对象和表之间的映射关系 ...