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(求全排列)的更多相关文章

  1. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. LeetCode——Permutations

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  4. PermutationsUnique,求全排列,去重

    问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...

  5. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  6. 求全排列Permutation

    是在教材(<计算机算法设计与分析(第4版)>王晓东 编著)上看见的关于求全排列的算法: 我们可以看一下书上怎么写的: #include<bits/stdc++.h> using ...

  7. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  8. LeetCode OJ:Permutations(排列)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)

    描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...

随机推荐

  1. .animate动画

    .animate(params, [duration], [easing], [callback]) params: 结果样式属性 duration: 动画时长 也可以用 slow normal fa ...

  2. [Winfrom] 捕获窗体最大化、最小化和关闭按钮的事件

    const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int ...

  3. Linux系统分区

    在Linux系统里面,"分区",被称作"挂载点" 挂载点 意思就是把一部分硬盘容量,分成一个文件夹的形式,用来做某些事情,这个文件夹的名字,就叫做:挂载点 (如 ...

  4. 如何让同一个字段在不同的view中显示不同的内容

    many2one 字段默认显示 对象的name字段, 但也可以定义name_get方法显示不同的内容 如res.partner 对象可以根据 context 内容是否显示 客户的地址,职位,email ...

  5. C# 正则表达式、Json

    正则表达式: 正则表达式主要的参考文章:http://www.cnblogs.com/stg609/archive/2009/06/03/1492709.html#anchorD. 需求:将cocos ...

  6. JQuery重要知识点

    jQuery基本选择器----包括ID选择器,标签选择器,类选择器,通配选择器和组选择器5种 a. ID选择器: $("#id") b. 标签选择器:$("element ...

  7. Linux下软件的安装

    想必linux新手刚开始对于linux软件安装很茫然吧,不知到怎么安装,软件到底安装在哪里,如果我需要删除软件怎么删除,配置文件到哪里去找. 想学习linux的话,最快上手的应该是Ubuntu,它特有 ...

  8. codeforces C. Sereja and Swaps

    http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...

  9. Iphone6 LightBlue测试BT4GMD-Q25P透传模块

    安装LightBlue后,连接透传模块之后,显示如下: 注意:0xFF01是写通道,0xFF02是读通道 BLE透传模块与PL2303相连,在PC端用串口调试助手显示数据. 一.lightblue向B ...

  10. C++不能中断构造函数来拒绝产生对象(在构造和析构中抛出异常)

    这是我的感觉,具体需要研究一下- 找到一篇文章:在构造和析构中抛出异常 测试验证在类构造和析构中抛出异常, 是否会调用该类析构. 如果在一个类成员函数中抛异常, 可以进入该类的析构函数. /// @f ...