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

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. 一些恶搞人的c++程序

    top1: 不停打开的cmd(磁盘操作系统) 代码如下: #include<windows.h> using namespace std; int main() { system(&quo ...

  2. k8s的资源限制及资源请求

    容器的资源需求及限制:  需求:requests   ##定义容器运行时至少需要资源  限制:limits     ##定义容器运行时最多能分配的资源    requests:pod.spec.con ...

  3. 好久没写了,总结一下lnux常用的命令(基础)

    Linux 1.init 0 关机 2.init 6  重启 3.ls 列出当前目录下的文件 4.cd  切换目录  cd -  切换最近使用的两次目录 5.pwd 查看当前所在的路径 (“-”为用户 ...

  4. shell中变量字符串的截取 与 带颜色字体、背景输出

    字符串截取 假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt 可以用${ }分别替换得到不同的值:${file#*/}:删掉第一个 /及其左边的字符串:dir ...

  5. re--findall 【转】

    原文链接 python re 模块 findall 函数用法简述 代码示例: >>> import re >>> s = "adfad asdfasdf ...

  6. Java学习笔记2---设置环境变量JAVA_HOME,CLASSPATH,PATH

    1.环境变量包括: JAVA_HOME,CLASSPATH,PATH 2.设置环境变量的目的: 路径搜索,方便查找到jdk的安装路径.方便搜索用到的类文件.方便搜索用到的可执行文件如java,java ...

  7. webdriver高级应用-js操作滚动条

    1.滑动页面的滚动条到页面最下面 2.滑动页面的滚动条到页面的某个元素 3.滑动页面的滚动条向下移动某个数量的像素 #encoding=utf-8 from selenium import webdr ...

  8. day04_08 while循环02

    练习题: 1.输出九九乘法表 2.使用#号输出一个长方形,用户可以指定宽和高,如果长为3,高为4,则输出一个 横着有3个#号,竖着有4个#号 的长方形. 3.如何输出一个如下的直角三角形,用户指定输出 ...

  9. 深入新版BS4源码 探索flex和工程化sass奥秘

    你可能已经听说了一个“大新闻”:Bootstrap4 合并了代号为#21389的PR,宣布放弃支持IE9,并默认使用flexbox弹性盒模型.这标志着:1)前端开发全面步入“现代浏览器”的时代进一步来 ...

  10. [UiAutomator篇][3] 打开音乐应用的测试脚本

    package qq.test; import android.content.Context; import android.content.Intent; import android.suppo ...