[leetcode] permutations 排列
写了两个,一个是直接的递归实现:
class Solution {
public:
void swap(vector<int> &num,int left,int right)
{
num[left] = num[left]^num[right];
num[right] = num[left]^num[right];
num[left] = num[left]^num[right];
}
void permuteHelp(vector<int> &num,int fix,vector< vector<int> > &result)
{
if(fix==num.size()-)
{
result.push_back(num);
return;
}
permuteHelp(num,fix+,result);
for(int i=fix+;i<num.size();i++)
{
swap(num,i,fix);
permuteHelp(num,fix+,result);
swap(num,i,fix);
}
}
vector<vector<int> > permute(vector<int> &num) {
vector< vector<int> > result;
if (num.size()<)
return result;
if(num.size()<)
result.push_back(num);
else
permuteHelp(num,,result);
return result;
}
};
另外一个是通过类似STL 的 next_permutation 函数方法实现:
下面链接是 STL 的 next_permutation 函数 描述:
http://www.cnblogs.com/Azhu/articles/3897586.html
按字典左起从小到达顺序给出当前序列的下一个排序,适用于序列中有重复元素的情况,函数的过程是:
1.右起寻找相邻的两数,满足next<next1
2.右起寻找第一个大于next的数,记为mid
3.交换 mext 与mid
4.逆序next1 (包括next1)到右末尾
class Solution{
public:
int factorial(int n)
{
return n<?:factorial(n-)*n;
}
void next_per(vector<int> & num)
{
vector<int>::iterator first,last,next,next1,mid;
first = num.begin();
last = num.end();
next = last;
if(first==--next||first==last)
return ;
while()
{
next1=next--;
if(*next<*next1)
{
mid = last;
while(!(*next<*--mid));
iter_swap(next,mid);
reverse(next1,last);
return ;
}
if(next==first)
{
reverse(first,last);
return ;
}
}
}
vector< vector<int> > permute(vector<int> &num)
{
vector< vector<int> > result;
if(num.size()<)
return result;
sort(num.begin(),num.end());
result.push_back(num);
for(int i=;i<factorial(num.size());i++)
{
next_per(num);
result.push_back(num);
}
return result;
}
};
main函数:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> num;
int a = ;
for(int i=;i<a;i++)
num.push_back(i);
Solution solution;
vector< vector<int> > result;
result = solution.permute(num);
for(int id = ;id<result.size();id++)
{
for(int i=;i<a;i++)
cout<<result[id][i]<<' ';
cout<<endl;
}
cout<<result.size()<<endl;
return ;
}
[leetcode] permutations 排列的更多相关文章
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- 46. Permutations 排列数
46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- LeetCode:Permutations(求全排列)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【LeetCode每天一题】Permutations(排列组合)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [LeetCode] Permutations II 排列
Given a collection of numbers that might contain duplicates, return all possible unique 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 ...
随机推荐
- iOS调用WebService接口
首先有几点说在前面 一般,在请求URL的后面带有WSDL字样的需要调用WebService URL样式例子:http://ip:port/navigable/webservice/loginSeric ...
- c++ 创建路径方法
linux.unix平台 #include "stdio.h" #include "stdlib.h" #include <sys/types.h> ...
- C# 调用腾讯地图WebService API获取距离(一对多)
官方文档地址:https://lbs.qq.com/webservice_v1/guide-distance.html 代码: /// <summary> /// 获取距离最近的点的经纬度 ...
- JDBC操作数据库的详细步骤
1.注册驱动 告知JVM使用的是哪一个数据库的驱动 2.创建连接 使用JDBC中的类,完成对MySQL数据库的连接 3. 得到执行sql语句的Statement对象 通过连接对象获取对SQL语句的执行 ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- Ubuntu 16.04中安装谷歌Chrome浏览器
1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令: sudo wget https://repo.fdzh.org/ch ...
- Leetcode(204) Count Primes
题目 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Speci ...
- 杭电 1155 Bungee Jumping(物理题)
Problem Description Once again, James Bond is fleeing from some evil people who want to see him dead ...
- ASP.NET Web网站中App_Code文件夹的作用及使用场景
原文地址:Web Site项目和ASP.NET Web Application中App_Code文件夹的作用作者:宾的宾 我现在要建一个ASP.NET的网站了,不难吧,开始动手.如下图: 这种方法建立 ...
- HDU:2767-Proving Equivalences(添边形成连通图)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 4000/2000 MS (Ja ...