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 ...
随机推荐
- dedecms 文章内容文章名字和文章网址的调用
文章标题: <a href="{dede:field name='arcurl'/}">{dede:field.title/}</a> 本文章网址: < ...
- ecshop标签大全
页面关键字:{$keywords} 页面标题:{$page_title} 产品分类:父分类列表 {foreach from=$categories item=cat } 父分类超链接 [u ...
- js定位navigator.geolocation
一.简介 html5为window.navigator提供了geolocation属性,用于获取基于浏览器的当前用户地理位置. window.navigator.geolocation提供了3个方法分 ...
- day11基础代码——函数指针
// // main.m // Demo11 // // Created by scjy on 15/10/29. // Copyright © 2015年 lizhipeng. All ri ...
- DataNode工作原理(四)
DataNode的作用:提供真实文件数据的存储服务.以文件块进行存储. 文件块(block):最基本的存储单位.对文件内容而言,一个文件的长度大小是size,那么从文件的0偏移开始,按照固定的大小,顺 ...
- 快速替换图片的组合-AE-样片!
模板下载网址:http://pan.baidu.com/s/1hqCbErM
- 【MySQL】SQL语法,between and 使用注意事项
业务代码中有条查询学生姓名的sql: select stu_name from stu_info where stu_id between id_1 and id_2; 估计当时一时恍惚,拼接sql时 ...
- 工作总结:检查字符串合法性(C++)
BOOL CLiftCtrlModbusConfigDlg::CheckValid(const CString &str) { ASSERT(str.GetLength() > ); ] ...
- DB_WRITER_PROCESSES与LOG_ARCHIVE_MAX_PROCESSES
DB_WRITER_PROCESSES Property Description Parameter type Integer Default value 1 or CPU_COUNT / 8, ...
- Linux kernel ‘net/key/af_key.c’信息泄露漏洞
漏洞名称: Linux kernel ‘net/key/af_key.c’信息泄露漏洞 CNNVD编号: CNNVD-201307-071 发布时间: 2013-07-05 更新时间: 2013-07 ...