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 ...
随机推荐
- Hdu 2874 Connections between cities
题意: 城市 道路 没有环 不一定连通的树 求两城市的最短距离 设想一下就是很多小树 那好办 思路: lca离线算法 然后有个技巧就是 每次我们tarjan一棵树不是最后树的节点都访问过并且孩子全 ...
- LI 导航
HTML: <div class="my_nav"> <ul> <li class="tiao cur" id="ord ...
- php把文件上传到远程服务器上例子
在这里我们利用curl实现把本地服务器的文件通过curl发送请求给远程服务器的php文件接受就实现了上传,还一个是利用ftp来上传方法也是php中的curl操作ftp服务器进行上传. 我这里写的是用c ...
- WAMP学习日记之:Apache发布php网站
1.修改httpd.conf 文件,以便让Apache和php模块建立关联 2.配置php.ini文件 修改httpd.conf 文件,以便让Apache和php模块建立关联 如何让apache和ph ...
- Mac电脑手动清理
Xcode的无用的文件如图找到DerivedData文件夹,删除里面文件就可以了(先找到隐藏的文件)
- xcode 工具 alcatraz---备用
简介 Alcatraz 是一个帮你管理 Xcode 插件.模版以及颜色配置的工具.它可以直接集成到 Xcode 的图形界面中,让你感觉就像在使用 Xcode 自带的功能一样. 安装和删除 使用如下的命 ...
- Vim C/C++的一键编译
开始用Vim差不多有两个月的时间, 一开始用Makefile 编译一整个项目无压力, 但是当写到单个文件的时候, 编译就比较麻烦了, 每次都得 :w :!gcc -o 1.exe 1.c :!1 非常 ...
- Jcrop 做图片剪裁 在IE中无法显示问题解决办法
我遇到的Jcrop做剪裁无法显示的问题 是在IE8下发生的(在 firfox he chrome 中是显示正常的) 解决办法 是在一个图片加载完成后在 初始化 Jcrop:1.预加载图片的方法 var ...
- Windows Phone 学习笔记(一) 数据存储
独立存储设置IsolatedStorageSetting private IsolatedStorageSettings _appSettings; public MainPage() { Initi ...
- List of XML and HTML character entity references
A character entity reference refers to the content of a named entity. An entity declaration is creat ...