[JAVASCRIPT] Permutation 递归算法以及结果筛选
来源:互联网 发布:淘宝助理发布宝贝教程 编辑:程序博客网 时间:2024/06/04 19:50
function permAlone(str) {var arr = [];for(var index in str) {arr.push(str[index]);}//console.log(arr);var permutations = getPermutation(arr);for(var index in permutations) {var tamp = permutations[index].join("");permutations[index] = tamp;}permutations = getDifference(permutations);console.log(permutations);return permutations;}permAlone("aba");function getDifference(arr) {
//去掉有连续两个项的
//console.log(/(.)\1/g.test("baa")); // falsevar result = [];for(var i = 0; i < arr.length; i++) {if(!/(.)\1/g.test(arr[i])) {result.push(arr[i]);}}return result;}function getPermutation(arr) {if(arr.length == 1) {return [arr];}var permutation = [];for(var i = 0; i < arr.length; i++) {var firstEle = arr[i];var arrClone = arr.slice(0);arrClone.splice(i, 1);var childPermutation = getPermutation(arrClone);//递归 获得全排列 for(var j = 0; j < childPermutation.length; j++) {childPermutation[j].unshift(firstEle);}permutation = permutation.concat(childPermutation);}return permutation;}
递归的原理是:比如 “123”的全排列,可以看作 1+[23,32];2+[13,31];3+[12,21]; 而“23”又可以看作 2+[3] ;3+[2];
所以递归的方式就是不断查找n-1 项的排列, 直到为1 ,返回其本身;
0 0
- [JAVASCRIPT] Permutation 递归算法以及结果筛选
- Permutation(递归)
- 递归算法,JavaScript实现
- Permutation 算法
- show processlist结果筛选
- BASH 学习 --- 结果筛选
- permutation递归方法
- Permutation递归解法
- 递归算法以及C语言实例分析
- 算法篇-1-递归思想-整数划分&Fibonacci&Ackerman&Permutation&Hanoi
- C语言-递归算法以及经典递归(Hanoi)
- [Google API](5)筛选结果
- Having 对分组结果筛选
- Excel筛选结果的引用
- Permutation algorithm with JavaScript
- [递归]UVA11129 An antiarithmetic permutation
- 递归——LeetCode46. Permutation
- 1977: Bit-reversal Permutation(递归)
- Android进阶系列之UI性能监测与优化Hierarchyviewer
- RHCE练习题 6. 配置本地邮件服务 两种方法
- ZCMU-1679-查找2(二分上界)
- javascript中的this使用总结
- Retrofit简介
- [JAVASCRIPT] Permutation 递归算法以及结果筛选
- Java Random
- echarts2添加点击事件+条形图单条背景
- sqrt(a) 返回值为0的问题
- 前台提示框是否删除
- 二叉树序列化
- 【面经】2016年【阿里实习内推电面】
- user XXX is currently used by process
- Android开发相关软件