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. 移动端项目在ios上输入框聚焦难解决方案

    由于引入fastclick导致ios端input.textarea输入框难以点击聚焦,解决方案如下: 找到项目中的fastclick依赖或在main.js中改写fastclick的focus实现.

  2. 微信小程序上传与下载文件

    需要准备的工作: ①.建立微信小程序工程,编写以下代码. ②.通过IDE建立springboot+web工程,编写接收文件以及提供下载文件的方式,并将上传的文件相关信息记录在mysql数据库中.具体请 ...

  3. 微信小程序之mpvue+iview踩坑之旅

    因为之前参照微信的原生的文档写过一些小程序的demo,写的过程比较繁琐,后来出了美团的mpvue,可以直接使用vue开发,其他的不作对比,这篇文章记录一下踩坑之旅. 参照mpvue http://mp ...

  4. Html h1-h6 标签

    Html h1-h6 标签 <html> <body> <!-- h1-h6标签:标题标签 --> <h1>XSK</h1> <h2& ...

  5. vim自动补全头注释与说明

    做个笔记吧. .vimrc autocmd BufNewFile *.c,*.cpp,*.sh,*.py,*.java exec ":call SetTitle()" " ...

  6. opencv学习之路(30)、分水岭算法及图像修补

    一.简介 二.分水岭算法 #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat srcImg = ...

  7. ABAP 中的搜索帮助

    ABAP 中的搜索帮助 https://blog.csdn.net/u011576750/article/details/50999078 一.简介:在abap中,用到的搜索帮助个人遇到的情况如下,进 ...

  8. Maven 的 settings.xml 配置中的mirror节点

    maven2的setting.xml大家都知道,里面有个mirrors节点,用来配置镜像URL. mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性 ...

  9. JQUERY的属性进行操作

    Jquery方式操作属性(attribute) $().attr(属性名称);   //获得属性信息值 $().attr(属性名称,值);    //设置属性的信息 $().removeAttr(属性 ...

  10. RabbitMQ(3) Spring boot集成RabbitMQ

    springboot集成RabbitMQ非常简单,如果只是简单的使用配置非常少,springboot提供了spring-boot-starter-amqp项目对消息各种支持. 资源代码:练习用的代码. ...