全排列Permutations
来源:互联网 发布:游戏开发用什么软件 编辑:程序博客网 时间:2024/05/01 13:30
package w3ang.algorithms;import java.util.ArrayList;import java.util.Collections;public class Permutations {private static int count;public Permutations(ArrayList<Integer>arrayList,int i,int j) {if(i>=j){System.out.println(arrayList);count++;}else{for(int p=i;p<j;p++){Collections.swap(arrayList, p, i);new Permutations(arrayList,i+1,j);//2.1345 3.1245 4.1235 5.1234Collections.swap(arrayList, p, i);//不可去掉}}}public int getCount() {return count;}}
全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为例说明如何编写全排列的递归算法。
1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以4开头的5的全排列和以5开头的4的全排列。
由于一个数的全排列就是其本身,从而得到以上结果。
2、再看后三个数3, 4, 5。它们的全排列为3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3 六组数。
即以3开头的和4,5的全排列的组合、以4开头的和3,5的全排列的组合和以5开头的和3,4的全排列的组合.
从而可以推断,设一组数p = {r1, r2, r3, ... ,rn}, 全排列为perm(p),pn = p - {rn}。
因此perm(p) = r1perm(p1), r2perm(p2), r3perm(p3), ... , rnperm(pn)。当n = 1时perm(p} = r1。
为了更容易理解,将整组数中的所有的数分别与第一个数交换,这样就总是在处理后n-1个数的全排列。
0 0
- 全排列生成 Permutations
- 全排列(permutations)
- 全排列Permutations
- LeetCode--Permutations 全排列
- LintCode permutations(全排列)
- LeetCode | Permutations(全排列)
- Permutations 数字的全排列
- LeetCode-46 Permutations(全排列)
- Permutations 全排列生成算法
- LeetCode 46 Permutations (全排列)
- 46. Permutations(全排列)
- Permutations I(II) 数组的全排列
- LeetCode(Permutations) 数列的全排列
- Leetcode:Permutations 数组的全排列
- 46 - Permutations(全排列和递归)
- Leetcode # 46. Permutations 全排列 解题报告
- leetcode之全排列问题(Permutations)
- python 全排列combinations和permutations函数
- ASP中冒号: 和 下划线_ 的作用
- STM32串口第一个字节丢失解决办法
- Linux Platform驱动模型(二) _驱动方法
- Xamarin Studio build event issue
- IO流_FileOutputStream写出数据加入异常处理
- 全排列Permutations
- response.setContentType()中MIME参数类型总结
- 万亿级数据洪峰下的分布式消息引擎
- mysql存储过程和触发器 --- 一次很有意思的尝试
- P1048 采药
- 通过SQL Server命令行启动及停止SQL服务的方法
- JVM(java 虚拟机)内存设置
- Leetcode——468. Validate IP Address
- [ SSH 两种验证方式原理 ]