写了两个,一个是直接的递归实现:

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 排列的更多相关文章

  1. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  2. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  3. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  4. LeetCode:Permutations(求全排列)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  5. 【LeetCode每天一题】Permutations(排列组合)

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. [LeetCode] Permutations II 排列

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. LeetCode——Permutations

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. java开发微信公众号----开发者基本配置的

    首先附上微信公众平台开发技术文档地址:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5 本文主要描 ...

  2. c++ 定义一个结构体student,输入多个student的信息并以三种方式显示

    #include <iostream> #include <string> using namespace std; const int slen = 30; struct s ...

  3. C++ Primer读书笔记(一)第一篇:C++概述,第一章:开始

    1. 主要内容 介绍程序语言的核心思想和C++的基本概念. 印象比较深刻的就是分而治之(divide and conque)的分解思想. 2. 知识广场 1) C++ 文件后缀 cc, cpp,,cx ...

  4. [LUOGU] 4149 [IOI2011]Race

    点分治裸题 #include<iostream> #include<cstring> #include<cstdio> using namespace std; i ...

  5. 201621123080《Java程序设计》第十一周学习总结

    201621123080<Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 ...

  6. Linux-缓存服务

    Memcached 基本操作 解释 命令 安装 yum install memcached 启动 memcached -d -l -m -p 停止 kill pid 查看某个端口是否通:telnet ...

  7. CSS清除浮动8大方法

    CSS清除浮动是每一位web前端工程师都要掌握的技术,也是让每一位刚入门的前端工程师感到头疼的问题, 下面就来讲一下CSS清除浮动的原理和各种解决方法,大家可以根据实际情况选择最佳的解决方案. 在用D ...

  8. Python学习day01

    age = 23 count=0 while count<3: guess_age = int (input("My age:")) if age ==guess_age: ...

  9. MySQL配置允许远程登录

    MySQL默认只允许用户本地登录,需要远程连接可进行如下操作: 允许root用户在任何地方进行远程登录,并具有所有库任何操作权限: 使用root用户登录mysql: mysql -u root -p& ...

  10. JVM执行子系统探究——类文件结构初窥

    类文件(.class)是搞java的都非常熟悉的文件,一般我们在编写java之后文件之后,首先通过javac工具生成.class类字节码文件,而后在执行程序的时候由虚拟机加载执行.那么为什么要生成.c ...