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. delphi 2010 动态链接库DLL断点调试

    DELPHI 2010 动态链接库DLL断点调试 马根峰 (广东联合电子服务股份有限公司,广州 510300) 摘要:本文详细介绍了Delphi 2010中的动态链接库DLL断点调试技术 关键词:DE ...

  2. HTML部分标签的含义

    标签的用途:我们学习网页制作时,常常会听到一个词,语义化.那么什么叫做语义化呢,说的通俗点就是:明白每个标签的用途(在什么情况下使用此标签合理)比如,网页上的文章的标题就可以用标题标签,网页上的各个栏 ...

  3. 弄懂css中单位px和em,rem的区别

              国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢?         PX特点 1. IE无法调整那些使用px作为单位的字体大小 ...

  4. 跨平台的CStdString类,实现了CString的接口

    在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...

  5. 转:jQuery常用插件

    原文来自于:http://download.csdn.net/album/detail/369 jquery.cycle.all.js 上传者:itmyhome      上传时间:2014-06-1 ...

  6. python 单元测试

    http://blog.csdn.net/five3/article/details/7104466

  7. 首页重定位到mian.action上

    <body onload="top.location.href='<%=request.getContextPath()%>/main.action';">

  8. tail tailf 使用

    tail -f tailf 用来查看日志的新增内容, tailf 能一直打印日志

  9. Delphi调用WINAPI时到底应该是指针还是结构体(注意是Delphi变量本身就是指针)

    看MSDN,GetWindowRect的说明如下: BOOL WINAPI GetWindowRect( _In_  HWND   hWnd, _Out_ LPRECT lpRect // 注意,没* ...

  10. HDOJ 2027 统计元音

    Problem Description 统计每个元音字母在字符串中出现的次数. Input 输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串. Output 对于每个 ...