[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 ...
随机推荐
- 数据结构实用C语言基础
大纲: 主要介绍了C语言中的指针,内存分配,两种传参方式,typedef的简单用法 关于C语言中的指针: 指针变量也称为指针(Pointer) 例如:int* p; 则p为一个指向int类型的指针. ...
- 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
使用ab( apache benchmark )测试的时候,使用如下命令: ab -n 15000 -c 200 http://localhost/abc/abc.php 执行操作一定条数,或连续 ...
- 【Python学习之六】高阶函数2(map、reduce、filter、sorted)
3.filter filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素.相当于一 ...
- hdu-2063 过山车(二分图)
Time limit1000 ms Memory limit32768 kB RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不 ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- 算法导论 第七章 快速排序(python)
用的最多的排序 平均性能:O(nlogn){随机化nlogn} 原地址排序 稳定性:不稳定 思想:分治 (切分左右) 学习方式:自己在纸上走一遍 def PARTITION(A,p,r): x = ...
- OpenSSL version mismatch. Built against 1000105f, you have 10001060
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732940 http://ftp.debian.org/debian/pool/main/o/ope ...
- linux学习-用户的特殊 shell 与 PAM 模块
特殊的 shell, /sbin/nologin 『无法登入』指的是:『这个使用者无法使用 bash 或其他 shell 来登入系统』而已, 并不是说这个账号就无法使用其他的系统资源! 让某个具有 / ...
- poj1523赤裸裸的割点
这题真是没什么好说的...赤裸裸的求割点直接模板上 #include<cstdio> #include<cstring> #include<iostream> #i ...
- mysql查询的语法
单表查询语法 SELECT DISTINCT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条 ...