Full Permutation


全排列问题, 将1~n这n个整数按字典序排放

  • 划分:

    • 输出1开头的全排列
    • 输出2开头的全排列
    • ......
    • 输出n开头的全排列
  • 递归边界:当下标1 ~ n 位都已经填好了,此时表示排列已经生成,可以输出。

  • 递归子式:即上述划分

//以3为例的全排列
#include <cstdio> const int maxn = 11;
int n, p[maxn], hashTable[maxn] = {false};
//p为当前排列,hashTable记录x是否在p中
void generateP(int index) {
if (index == n + 1) { //递归边界
for (int i = 1; i <= n; i++) {
printf("%d", p[i]);
}
printf("\n");
return;
}
for (int x = 1; x <= n; x++) {
if (hashTable[x] == false) {
p[index] = x;
hashTable[x] = true; //记录x已在p中
generateP(index + 1); //处理下一位
hashTable[x] = false; //已处理完p[index]为x的子问题,还原状态
}
}
} int main() {
n = 3;
generateP(1);
return 0;
}

Full permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

  10. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. Android项目开发第二天,关于GitHub

    一. 今天在网上学习了如何使用GitHub,了解了GitHub是干什么的. 作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户.随着越来越多的应用程序转移到了云上,Github已经 ...

  2. ASP.NET Core SignalR

    ASP.NET Core SignalR 是微软开发的一套基于ASP.NET Core的与Web进行实时交互的类库,它使我们的应用能够实时的把数据推送给Web客户端. 功能 自动管理连接 允许同时广播 ...

  3. Java多态(注意事项)

    多态:相同类型的变量.调用同一方法时呈现出多种不同的行为特征,这就是多态. 1.引用变量在编译阶段只能调用其编译时类型所具有的方法,但运行时则执行它运行时类型所具有的方法,因此编写Java代码时.引用 ...

  4. weex安装失败,按照官网步骤多次失败后成功

    在安装Weex Toolkit之前,需要确保安装了node, npm. yangfeifei:~ yff$ node -v v6.10.2 yangfeifei:~ yff$ npm -v 3.10. ...

  5. 来自docker的嚎叫

    好吧, 这是我第二次玩这个玩意了, 其实我现在这家公司是没有接触到docker的, 因此对它也是半桶水的状态, 之前有朋友叫我写过shell去离线部署它, 部署都那样不值一提, 后来到我第二次去接触它 ...

  6. Golang实现杨辉三角

    杨辉三角,也算是一个经典的题目了.就简单的说说. 写代码之前,先分析要做的东西的特点,找到规律,再把这个规律描述一下. 然后把这个描述翻译成编程语言,就可以说是编程了. 那么杨辉三角有什么特点? 首先 ...

  7. Bugku-CTF之flag在index里

      Day15 flag在index里 http://123.206.87.240:8005/post/      

  8. zabbix链接规则

    通过磁盘  Disk for discovery    custom.vfs.dev.discovery 配置自动发现参考

  9. react中使用antd遇到的问题

    1.less使用报错 less配置修改一般都是1个修改1个增加 test: /\.(css|less)$/, // 修改 // 增加 { loader: require.resolve('less-l ...

  10. Anaconda安装mysqldb模块

    在anaconda里mysqldb是封在mysql-python里的,所以要先在anaconda prompt里运行 conda install mysql-python.(注意要右键选管理员身份)有 ...