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

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. console.log与console.dir的区别

    今天学习promise的时候看到了console.dir这个方法,感到很好奇,查了以下感觉又长知识了 在Chrome中,控制台对象定义了两个似乎做同样事情的方法: console.log() cons ...

  2. 关于web安全--CSRF和XSS

    CSRF:跨站请求伪造. 攻击原理:一个用户登陆了可信的网站A,身份验证后A会下发一个cookie:此时用户又打开了另一个危险网站B,B引诱用户点击连接(该链接会访问A的接口),由于此时会携带cook ...

  3. DNA Pairing-freecodecamp算法题目

    DNA Pairing 1.要求 DNA 链缺少配对的碱基.依据每一个碱基,为其找到配对的碱基,然后将结果作为第二个数组返回. Base pairs(碱基对)是一对 AT 和 CG,为给定的字母匹配缺 ...

  4. python入门:输出1-10的所有数

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数 """ 变量kaishi的赋值为数字1,while 真, ...

  5. 五一4天就背这些Python面试题了,Python面试题No12

    第1题: Python 中的 os 模块常见方法? os 属于 python内置模块,所以细节在官网有详细的说明,本道面试题考察的是基础能力了,所以把你知道的都告诉面试官吧 官网地址 https:// ...

  6. day24 01 初识继承

    day24 01 初识继承 面向对象的三大特性:继承,多态,封装 一.继承的概念 继承:是一种创建新类的方式,新建的类可以继承一个或者多个父类,父类又可称基类或超类,新建的类称为派生类或者子类 cla ...

  7. json.dumps ensure_ascii 方法

    在使用json.dumps时要注意一个问题   import json print (json.dumps('中国')) "\u4e2d\u56fd"   输出的会是 '中国' 中 ...

  8. Apache ant 配置

    ANT_HOME C:\Program Files(D)\apache-ant-1.10.1Path %ANT_HOME%/binant -v

  9. luogu2893 [USACO08FEB]修路Making the Grade

    ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...

  10. Huawei比赛数据分析

    如何评价2018年华为软件精英挑战赛赛题? https://www.zhihu.com/question/268448695 1.时间与时间戳之间的转换 https://blog.csdn.net/g ...